How to send 'data' to ASMX web service via AJAX POST?
16,604
Solution 1
For asmx you need to pass a stringified version of the data object, so for example:
var data = "{param1:" + param1IsANumber +
", param2:\"" + param2IsAString + "\"}";
$.ajax({
data: data,
dataType: "json",
url: url,
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (result) {}
});
Or you can hava an object and use jquery-json
var data = {};
data.param1 = 1;
data.param2 = "some string";
$.ajax({
data: jQuery.toJSON(data),
dataType: "json",
url: url,
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (result) {}
});
Finally, your web service class must look like:
[WebService(Namespace = "http://www.somedomainname.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class MyService : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void MyServiceCall(int param1, string param2)
{
}
}
Solution 2
jQuery takes the data argument and converts it into the appropriate type of request variables.
So you use something like:
data: { myParameterName: "myParameterValue", myParameterName2: "myParameterValue2" }
and jQuery does the rest of the work for you.
A specific example based on a comment:
data: { toSend: "test" }
Author by
Mr-DC
Updated on June 05, 2022Comments
-
Mr-DC almost 2 years
I can successfully receive values from my web service so in that repect the script is working fine. However I am now trying to send data to the webservice using the 'data' field below. What i cant figure out is how to send a simple string (e.g. "test") to the web service which is what my web method expects as a parameter.
Any help is much appreciated. For example:
function setQuestion() { $.ajax({ type: "POST", **data: "{}",** //how do i use this to send a string?? dataType: "json", url: "http://someURL", contentType: "application/json; charset=utf-8", success: onSuccess }); } function onSuccess(msg) { $("#questiontxt").append(msg); }
-
Mr-DC about 12 yearsThanks for that Dave, however I still get an exception: "Invalid JSON Primitive: value"...
-
Mr-DC about 12 yearsHi petrov, I get the "Invalid JSON Primitive" exception for your solution too. Also do i need a method in the web service called 'GetData' to retrieve that data?
-
Dave about 12 yearsCan you post the declaration for the ASMX function you're trying to post to?
-
Mr-DC about 12 yearsIts ok dave, this way worked: data: '{"value": "test"}',
-
Dave about 12 yearsThat's odd, you shouldn't need to send the data as a string. But I guess if it's working....
-
petrov.alex about 12 yearsok, my solution is not working because I used the same quotes
-
Mr-DC about 12 yearsYea, actually i do have another question now, what if i wanted to send the value of a variable like this:
var toSend = "test";
data: '{"value": toSend}',
Note this doesnt work, i just tried :( -
Dave about 12 yearsUpdated my answer to reflect your specific parameter name and value.
-
cdm9002 about 12 yearsDave, you must send as a string for asmx. Davey1990, use $.toJSON()..see my answer below.
-
cdm9002 about 12 yearsGreat. You are actually supposed to quote the param names, e.g. "{\"param1\":" + p1 + "}" but it does actually work without :)