0

I have made a getdata webmethod which returns the data

    function GetService() {
       debugger;
//        var prod = $('#txt_num1').val();
//        var price = $('#txt_num2').val();
//        var active = $('#txt_num3').val();

        $.ajax({
            type: "POST",
            url: "WebService1.asmx/getdata",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            error: OnError,
        });

        function OnSuccess(data, status) {
            $("#lblResult").removeClass("loading");
            $("#lblResult").html($.parseJSON(data.d));
        }

        function OnError(request, status, error) {
            $("#lblResult").removeClass("loading");
            $("#lblResult").html(request.statusText);

        }
    }

Where am I going wrong? How can i display data from the webservice through jquery in div?

10
  • stackoverflow.com/questions/1637334/… Commented Jul 26, 2013 at 9:16
  • not need use $.parseJSON(data.d) because dataType: "json" already parse json, data is js object, just $("#lblResult").html(data.d); Commented Jul 26, 2013 at 9:33
  • but how cani show multiple data ? Commented Jul 26, 2013 at 9:36
  • show your json format from server Commented Jul 26, 2013 at 9:38
  • iam getting internal server error msg. Commented Jul 26, 2013 at 9:38

1 Answer 1

1

Serialize your response:

            [WebMethod]
            public HttpResponseMessage getdata()
            {
                string conn = "Data Source=.\\sqlexpress; initial catalog=Test; user id=sa; pwd=manager;"; 
                SqlConnection connection = new SqlConnection(conn); 
                connection.Open(); 
                SqlDataAdapter da = new SqlDataAdapter("Select * from PRODUCT", connection); 
                DataSet dt = new DataSet(); 
                da.Fill(dt); 
                connection.Close();

                var resp = new HttpResponseMessage()
                {
                    Content = new StringContent(new JavaScriptSerializer().Serialize(dt))
                };
                resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                return resp;
            }

JS:

function OnSuccess(data) {
            console.log(data);
        }

function OnError(request, status, error) {
            console.trace();
        }

function GetService() {

        $.ajax({
            type: "POST",
            url: "WebService1.asmx/getdata",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            error: OnError,
        });
}
Sign up to request clarification or add additional context in comments.

1 Comment

and how can i send it in xml ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.