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.
How to solve Invalid JSON Primitive error on Ajax?
Your postData is invalid JSON, a string needs quotes around it:
'{fileName:'+name+'}'
It should be:
'{fileName:"'+name+'"}'
Invalid JSON primitive On Post
When your specify contentType: "application/json
, you must stringify the data you send as JSON. In your case, the data is a javascript objec` (which contains some stringified data), not JSON.
Since userdetails
is already stringified (using JSON.stringify()
), then change the ajax code to
$.ajax({
url: url,
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: userdetails,
success: function (data) {
...
});
However, there is no need to send it as JSON. You can use the default 'application/x-www-form-urlencoded; charset=UTF-8'
Delete the userdetails = JSON.stringify(userdetails);
line of code, and then its just
$.ajax({
url: url,
type: 'POST',
dataType: 'json',
data: userdetails,
success: function (data) {
...
});
Note in both cases, you can omit the traditional: false
option.
And if you have generated your form controls correctly using the HtmlHelper
methods to bind to your model properties, then you do not need to manually create the object
. You can simply use .serialize()
. In addition, you should be handling the forms submit event, not the button click event, and checking .valid()
$('form').submit(function(e) {
e.preventDefault();
if (!$(this).valid()) {
return;
}
var userdetails = $(this).serialize();
var url = '@Url.Action("WFUserEdited", "WFUser")';
$.ajax({
url: url,
type: 'POST',
dataType: 'json',
data: userdetails,
success: function (data) {
...
});
});
Invalid JSON primitive ERROR
You are promising a content type of application/json
but are sending a plain JS Object, which gets serialised as percentile-encoded-string by jQuery. This serialization might be far from valid JSON.
Change:
data: {'partyId':party,'PartySelCombo':valueFrom,'DocumentId':DocId},
to:
data: JSON.stringify({'partyId':party,'PartySelCombo':valueFrom,'DocumentId':DocId}),
Getting Invalid JSON primitive error from MVC AJAX call
You posting back an object, not json, so you need to remove the contentType: 'application/json;',
ajax option so that it uses the default (which is 'application/x-www-form-urlencoded; charset=UTF-8'
) Alternatively you need to use JSON.stringify() to convert the object to a JSON string.
Side note: It is not necessary to quote the property names. It can be
var dataObject = {
FirstName: $('#FirstName').val(),
....
Invalid JSON primitive when sending POST request
Your issue is because you're not sending valid JSON. You need to wrap the keys in double quotes. Also, subscriptionJson
is a literal string in your code. You need it to be a string variable.
data: '{"subscriptions": ' + subscriptionJson + '}',
Or, even better, just provide a plain object to jQuery and let it encode the values for you:
data: { subscriptions: subscriptionJson },
Jquery Ajax POST to C# WebMethod Errors with Invalid JSON primitive: System.Object.
public static string SubData(Selection item)
{
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
//ERROR OCCURS HERE
var selection = serializer.Deserialize(item.ToString());
return "this is successful";
}
Here, item
is not a string (and thus not the JSON being sent). Since you're calling ToString()
on it, it's likely the library is trying to deserialize text similar to System.Object
- which will fail.
At a quick glance at the code, it looks like item
is already deserialized for you, so you don't need to do anything further
Related Topics
Servicestack Rest API and Cors
How to Loop Through Ienumerable in Batches
What Is the Equivalent of Memset in C#
Convert JSON String to JSON Object C#
Read Post Data Submitted to ASP.NET Form
Can a C# Class Inherit Attributes from Its Interface
"Invalid JSON Primitive" in Ajax Processing
Correct, Idiomatic Way to Use Custom Editor Templates with Ienumerable Models in ASP.NET MVC
What Does {0} Mean When Found in a String in C#
How to Determine a Mapped Drive's Actual Path
Listen to Changes of Dependency Property
Capture Screenshot Including Semitransparent Windows in .Net
A Property or Indexer May Not Be Passed as an Out or Ref Parameter