I'm trying to post some simple parameters to a .asmx webservice.
I get the following error: Request format is invalid: application/json; charset=utf-8.
What I really need to get to is to be able to pass a complex object, but I can't get past making a POST request with json content type.
Here is my WebService Definition
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public int JsonTest2(int myparm1, int myparm2)
{
return 101;
}
And this is my javascript code
function JsonTest2() {
$.ajax({
type: 'POST',
url: "http://localhost/WebServices/MyTest.asmx/JsonTest2",
data: "{myparm1:105,myparm2:23}",
contentType: 'application/json; charset=UTF-8',
dataType: 'json',
async: false,
success: function (msg) {
alert(msg);
},
error: function (msg) {
alert('failure');
alert(msg);
}
});
}
You should use as
data
the value which formatted as correct JSON data:instead of
You can validate on the site http://www.jsonlint.com/ which data are JSON data. So you should change your code to
In case of more complex input parameters I recommend you to use
JSON.stringify
functionfrom the json2.js (see this answer for example):In the last version of
$.ajax
usage themyValue1
andmyValue2
can be complex structures (objects with properties) or arrays having even another complex structures or arrays as the properties.Make sure your ASMX service class is decorated with the
[ScriptService]
attribute.Make sure the URL contains port number when using localhost.