I am trying to post a JSON object to a asp.net webservice.
My json looks like this:
var markers = { "markers": [
{ "position": "128.3657142857143", "markerPosition": "7" },
{ "position": "235.1944023323615", "markerPosition": "19" },
{ "position": "42.5978231292517", "markerPosition": "-3" }
]};
I am using the json2.js to stringyfy my json object.
and i am using jquery to post it to my webservice.
$.ajax({
type: "POST",
url: "/webservices/PodcastService.asmx/CreateMarkers",
data: markers,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data){alert(data);},
failure: function(errMsg) {
alert(errMsg);
}
});
I am getting the following error:
"Invalid JSON primitive:
I have found a bunch of posts relating to this and it seems to be a really common problem but nothing i try fixes the issue.
When firebug what is being posted to the server it looks like this:
markers%5B0%5D%5Bposition%5D=128.3657142857143&markers%5B0%5D%5BmarkerPosition%5D=7&markers%5B1%5D%5Bposition%5D=235.1944023323615&markers%5B1%5D%5BmarkerPosition%5D=19&markers%5B2%5D%5Bposition%5D=42.5978231292517&markers%5B2%5D%5BmarkerPosition%5D=-3
My webservice function that is being called is:
[WebMethod]
public string CreateMarkers(string markerArray)
{
return "received markers";
}
markers
is not a JSON object. It is a normal JavaScript object.data:
option:If you want to send the data as JSON, you have to encode it first:
jQuery does not convert objects or arrays to JSON automatically.
But I assume the error message comes from interpreting the response of the service. The text you send back is not JSON. JSON strings have to be enclosed in double quotes. So you'd have to do:
I'm not sure if your actual problem is sending or receiving the data.
Please follow this to by ajax call to webservice of java var param = { feildName: feildValue }; JSON.stringify({data : param})
I have encountered this one too and this is my solution.
If you are encountering an invalid json object exception when parsing data, even though you know that your json string is correct, stringify the data you received in your ajax code before parsing it to JSON:
You mentioned using json2.js to stringify your data, but the POSTed data appears to be URLEncoded JSON You may have already seen it, but this post about the invalid JSON primitive covers why the JSON is being URLEncoded.
I'd advise against passing a raw, manually-serialized JSON string into your method. ASP.NET is going to automatically JSON deserialize the request's POST data, so if you're manually serializing and sending a JSON string to ASP.NET, you'll actually end up having to JSON serialize your JSON serialized string.
I'd suggest something more along these lines:
The key to avoiding the invalid JSON primitive issue is to pass jQuery a JSON string for the
data
parameter, not a JavaScript object, so that jQuery doesn't attempt to URLEncode your data.On the server-side, match your method's input parameters to the shape of the data you're passing in:
You can also accept an array, like
Marker[] Markers
, if you prefer. The deserializer that ASMX ScriptServices uses (JavaScriptSerializer) is pretty flexible, and will do what it can to convert your input data into the server-side type you specify.I tried Dave Ward's solution. The data part was not being sent from the browser in the payload part of the post request as the contentType is set to
"application/json"
. Once I removed this line everything worked great.I have query,
I'm posting the the login details in json and getting a string as
"Success"
,but I'm not getting the response.