c# - How do I build a JSON object to send to an AJAX WebService? -


after trying format json data hand in javascript , failing miserably, realized there's better way. here's code web service method , relevant classes looks in c#:

[webmethod] public response validateaddress(request request) {     return new test_addressvalidation().generateresponse(         test_addressvalidation.responsetype.ambiguous); }  ...  public class request {     public address address; }  public class address {     public string address1;     public string address2;     public string city;     public string state;     public string zip;     public addressclassification addressclassification; }  public class addressclassification {     public int code;     public string description; } 

the web service works great using soap/xml, can't seem valid response using javascript , jquery because message server has problem hand-coded json.

i can't use jquery getjson function because request requires http post, i'm using lower-level ajax function instead:

$.ajax({     type: "post",     contenttype: "application/json; charset=utf-8",     url: "http://bmccorm-xp/hbupsaddressvalidation/addressvalidation.asmx/validateaddress",     data: "{\"address\":{\"address1\":\"123 main street\",\"address2\":null,\"city\":\"new york\",\"state\":\"ny\",\"zip\":\"10000\",\"addressclassification\":null}}",     datatype: "json",     success: function(response){         alert(response);     } }) 

the ajax function submitting specified in data:, problem is. how build formatted json object in javascript can plug in ajax call so:

data: therequest 

i'll pulling data out of text inputs in forms, hard-coded test data fine.

how build formatted json object send web service?


update: turns out problem request wasn't formatting of json, t.j. pointed out, rather json text didn't conform requirements of web service. here's valid json request based on code in webmethod:

'{"request":{"address":{"address1":"123 main street","address2":"suite 20","city":"new york","state":"ny","zip":"10000","addressclassification":null}}}' 

this brought question: when case sensitivity important in json requests asp.net web services (asmx)?

the answer easy , based on previous posts can return json .asmx web service if contenttype not json? , jquery ajax call httpget webmethod (c#) not working.

the data should json-encoded. should separate encode every input parameter. because have 1 parameter should following:

first construct data native javascript data like:

var mydata = {address: {address1:"address data 1",                         address2:"address data 2",                         city: "bonn",                         state: "nrw",                         zip: "53353",                         {code: 123,                          description: "bla bla"}}}; 

then give parameter of ajax request {request:$.tojson(mydata)}

$.ajax({     type: "post",     contenttype: "application/json; charset=utf-8",     url: "http://bmccorm-xp/hbupsaddressvalidation/addressvalidation.asmx/validateaddress",     data: {request:$.tojson(mydata)},     datatype: "json",     success: function(response){         alert(response);     } }) 

instead of $.tojson come json plugin can use version (json.stringify) http://www.json.org/

if webmethod had parameters like

public response validateaddress(request request1, request myrequest2) 

the value of data parameter of ajax call should like

data: {request1:$.tojson(mydata1), myrequest2:$.tojson(mydata2)} 

or

data: {request1:json.stringify(mydata1), myrequest2:json.stringify(mydata2)} 

if prefer version of json encoder.


Comments

Popular posts from this blog

Delphi Wmi Query on a Remote Machine -