asmx web service, json, javascript/jquery?

12,503

Solution 1

I recommend you look my previous answer for the close questions How do I build a JSON object to send to an AJAX WebService? and Can I return JSON from an .asmx Web Service if the ContentType is not JSON?

The correct code should looks like following

[WebMethod]
[ScriptMethod (ResponseFormat = ResponseFormat.Json)]
public EntityLayer.TestPage1 GetData(int id)
{
    TestPage1 test = TestPage1.GetData(id).SingleOrDefault();
    return test;
}

and

var myData = 5;
$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "WebService.asmx/GetData",
    //data: {id:JSON.stringify(myData)},
    data: JSON.stringify({id:myData}),
    dataType: "json",
    success: function(response){
        alert("UserName=" + response.d.UserName +
              ", FirstName=" + response.d.FirstName +
              ", MiddleName=" + response.d.MiddleName+
              ", LastName=" + response.d.LastName);
    }
})

where JSON.stringify is a function from the script json2.js which you can download from http://www.json.org/js.html.

If the id values are integer JSON.stringify(myData) are the same as myData, but for all more complex examples I strictly recommend you to use this function.

How you can also see from the code the all results of the web method will be saved in the property d, so you should use for example response.d.FirstName syntax to access the first name.

UPDATED: In case of HTTP GET the data parameter should be {id:JSON.stringify(myData)}. In case of HTTP POST: JSON.stringify({id:myData})

Solution 2

A couple things...

Some untested sample code:

$.ajax({
  type: "POST",
  contentType: "application/json; charset=utf-8",
  url: "WebService.asmx/GetData",
  data: "{id}",
  dataType: "json"
  success: function(data) {
    var str = '' +
      'UserName: ' + data.UserName + '\n' +
      'Password: ' + data.Password + '\n' +
      'FirstName: ' + data.FirstName + '\n' +
      'LastName: ' + data.LastName + '\n' +
      'MiddleName: ' + data.MiddleName;
    alert(str);
  }
});
Share:
12,503
Bart
Author by

Bart

Updated on June 04, 2022

Comments

  • Bart
    Bart almost 2 years

    I am using asmx to retrieve some data from DB,

    public class TestPage1
    {
        public int UserID { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string MiddleName { get; set; }
    }
    
    
    
    
        [WebMethod]
        public EntityLayer.TestPage1 GetData(int id)
        {
            TestPage1 test = TestPage1.GetData(id).SingleOrDefault();
            return test;
        }
    
    
    $.ajax({
      type: "POST",
      contentType: "application/json; charset=utf-8",
      url: "WebService.asmx/GetData",
      data: "{id}",
      dataType: "json"
    });
    

    How Do I desrialize test object in javascript?? and is there a better way? thanks

  • nikib3ro
    nikib3ro almost 12 years
    And anyone who wants to try this out - do not forget to mark your WebService class with [ScriptService] - just had problem with that.