Monday, May 20, 2024
Homepage · c#
 Popular · Latest · Hot · Upcoming
177
rated 0 times [  182] [ 5]  / answers: 1 / hits: 30888  / 14 Years ago, thu, april 29, 2010, 12:00:00

After trying to format my JSON data by hand in javascript and failing miserably, I realized there's probably a better way. Here's what the code for the web service method and relevant classes looks like 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 with using SOAP/XML, but I can't seem to get a valid response using javascript and jQuery because the message I get back from the server has a problem with my hand-coded JSON.



I can't use the jQuery getJSON function because the request requires HTTP POST, so I'm using the 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 is submitting everything specified in data:, which is where my problem is. How do I build a properly formatted JSON object in javascript so I can plug it in to my ajax call like so:



data: theRequest


I'll eventually be pulling data out of text inputs in forms, but for now hard-coded test data is fine.



How do I build a properly formatted JSON object to send to the web service?






UPDATE: It turns out that the problem with my request wasn't the formatting of the JSON, as T.J. pointed out, but rather that my JSON text didn't conform to requirements of the web service. Here's a valid JSON request based on the code in the WebMethod:



'{request:{Address:{Address1:123 Main Street,Address2:suite 20,City:New York,State:NY,Zip:10000,AddressClassification:null}}}'


This brought up another question: When is case sensitivity important in JSON requests to ASP.NET web services (ASMX)?


More From » c#

 Answers
15

The answer is very easy and based on my previous posts Can I return JSON from an .asmx Web Service if the ContentType is not JSON? and JQuery ajax call to httpget webmethod (c#) not working.



The data should be JSON-encoded. You should separate encode every input parameter. Because you have only one parameter you should do like following:



first construct you data as 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 as a 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 which come from the JSON plugin you can use another version (JSON.stringify) from http://www.json.org/



If your WebMethod had parameters like



public Response ValidateAddress(Request request1, Request myRequest2)


the value of data parameter of the ajax call should be like



data: {request1:$.toJSON(myData1), myRequest2:$.toJSON(myData2)}


or



data: {request1:JSON.stringify(myData1), myRequest2:JSON.stringify(myData2)}


if you prefer another version of JSON encoder.


[#96927] Tuesday, April 27, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
gideons

Total Points: 197
Total Questions: 106
Total Answers: 108

Location: Moldova
Member since Sat, Jan 29, 2022
2 Years ago
;