c# – Invalid JSON primitive in Ajax processing
c# – Invalid JSON primitive in Ajax processing
Just a guess what does the variable json
contain after
var json = Sys.Serialization.JavaScriptSerializer.serialize(obj);?
If it is a valid json object like {foo:foovalue, bar:barvalue}
then jQuery might not send it as json data but instead serialize it to foor=foovalue&bar=barvalue
thus you get the error Invalid JSON primitive: foo
Try instead setting the data as string
$.ajax({
...
data: {foo:foovalue, bar:barvalue}, //note the additional quotation marks
...
})
This way jQuery should leave the data alone and send the string as is to the server which should allow ASP.NET to parse the json server side.
Using
data : JSON.stringify(obj)
in the above situation would have worked I believe.
Note: You should add json2.js library all browsers dont support that JSON object (IE7-)
Difference between json.js and json2.js
c# – Invalid JSON primitive in Ajax processing
its working
something like this
data: JSON.stringify({id:x}),