Send JSON for web method?

How can I send a JSON object to a web method using jQuery?

+7
json jquery c # asmx
source share
7 answers

Please refer to this article by Dave Ward. This is a complete tutorial about this. You will also find other great jquery / ASP.net there.

EDIT: - Dave calls the method without any arguments, you can replace the empty data strong> property with the actual data you want to send:

$.ajax({ type: "POST", url: "Default.aspx/GetDate", data: "{'name':'tiger1','hobbies':['reading','music']}",//PUT DATA HERE contentType: "application/json; charset=utf-8", dataType: "json", 
+20
source share

WebMethods expects a string containing JSON to be parsed on the server side, I use the JSON.stringify function to convert the parameter object to a string and send data, I have this function:

 jQuery.executePageMethod = function(location, methodName, methodArguments, onSuccess, onFail) { this.ajax({ type: "POST", url: location + "/" + methodName, data: JSON.stringify(methodArguments), // convert the arguments to string contentType: "application/json; charset=utf-8", dataType: "json", success: function(data, status) { var jsonData = JSON.parse(data.d); onSuccess(jsonData, status); }, fail: onFail }); }; 

I recommend that you include the json2.js analyzer in your pages so that you can use the cross-browser JSON.stringify.

+11
source share

Another library you can use is jquery-json library . After turning on:

 var json = $.toJSON(your_object); 
+6
source share

The most convenient solutions I've seen simplify this by using the open source library JSON2.js to parse and โ€œplanโ€ a complex data object.

These two great articles are covered in detail:

The second article may be especially relevant for you, although it calls the web service method with the following signature ...

 public void SendValues(List<string> list) 

... it demonstrates how to use the JSON2.js library to render List<string> in javascript (using jQuery, this example is taken directly from the second article):

 var list = ["a", "b", "c", "d"]; var jsonText = JSON.stringify({ list: list }); // The 'list' is posted like this $.ajax({ type: "POST", url: "WebService1.asmx/SendValues", data: jsonText, contentType: "application/json; charset=utf-8", dataType: "json", success: function() { alert("it worked"); }, failure: function() { alert("Uh oh"); } }); 

Just use your web method URL instead of the web service.

+5
source share

You will need to publish it using Ajax and accept the input string on the web method. Then you will need to use the JavaScript deserializer to convert it to an object on the server side.

0
source share

JSON.stringify helps, but:

0
source share

Sample code here:

 var dataString = JSON.stringify({ contractName: contractName, contractNumber: contractNumber }); $.ajax({ type: "POST", url: "CreateQuote.aspx/GetCallHistory", data: dataString, contentType: "application/json; charset=utf-8", dataType: "json", success: function (result) { alert(result.CallHistoryDescription); OpenLightBox('divDelete'); } }); [System.Web.Services.WebMethod] public static object GetCallHistory(string contractName, string contractNumber) { return new { CallHistoryDescription = "Nalan" }; } 
0
source share

All Articles