“Invalid JSON primitive” in Ajax processing

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



“Invalid JSON primitive” in Ajax processing



I am getting an error in an ajax call from jQuery.



Here is my jQuery function:


function DeleteItem(RecordId, UId, XmlName, ItemType, UserProfileId)
var obj =
RecordId: RecordId,
UserId: UId,
UserProfileId: UserProfileId,
ItemType: ItemType,
FileName: XmlName
;
var json = Sys.Serialization.JavaScriptSerializer.serialize(obj);

$.ajax(
type: "POST",
url: "EditUserProfile.aspx/DeleteRecord",
data: json,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function(msg)
if (msg.d != null)
RefreshData(ItemType, msg.d);

,
error: function(XMLHttpRequest, textStatus, errorThrown)
alert("error occured during deleting");

);



and this is my WebMethod:


WebMethod


[WebMethod]
public static string DeleteRecord(Int64 RecordId, Int64 UserId, Int64 UserProfileId, string ItemType, string FileName)
try
string FilePath = HttpContext.Current.Server.MapPath(FileName);

XDocument xmldoc = XDocument.Load(FilePath);
XElement Xelm = xmldoc.Element("UserProfile");
XElement parentElement = Xelm.XPathSelectElement(ItemType + "/Fields");

(from BO in parentElement.Descendants("Record")
where BO.Element("Id").Attribute("value").Value == RecordId.ToString()
select BO).Remove();
XDocument xdoc = XDocument.Parse(Xelm.ToString(), LoadOptions.PreserveWhitespace);
xdoc.Save(FilePath);

UserInfoHandler obj = new UserInfoHandler();
return obj.GetHTML(UserId, UserProfileId, FileName, ItemType, RecordId, Xelm).ToString();
catch (Exception ex)
HandleException.LogError(ex, "EditUserProfile.aspx", "DeleteRecord");

return "success";



Can anybody please tell me what's wrong in my code?



I am getting this error:



"Message":"Invalid JSON primitive: RecordId.",
"StackTrace":"
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)
at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)
at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)
at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)
at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)
at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)",
"ExceptionType":"System.ArgumentException"





What I don't understand is. The javascript is about AddAlbumToMyProfile while the WebMethod is called DeleteRecord. Are you sure you show us the right code pieces?
– jitter
Mar 15 '10 at 9:13





Any chance you can also capture what the POST looks like (using firebug or whatnot) and add it to the question? I'm not sure if it's the way you're encoding the data before sending it, but you could also try serializing it using this( json.org/json2.js ).
– R0MANARMY
Mar 21 '10 at 21:31




10 Answers
10



Just a guess what does the variable json contain after


json


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"


'foo':'foovalue', 'bar':'barvalue'


foor=foovalue&bar=barvalue


"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.





thanks for the clarification, add one more comment, you can always do like JSON.stringify(foo:'foovalue', bar:'barvalue') for a easier life
– Elaine
Jul 1 at 12:26



Using


data : JSON.stringify(obj)



in the above situation would have worked I believe.



Note: You should add json2.js library all browsers don't support that JSON object (IE7-)
Difference between json.js and json2.js





Thanks! When using simple JS classes this works. I changed data: JSON.stringify(obj) to data: JSON.stringify(obj) (My javascript/JSON class to serialize is of the style var myObj = title: "x", subclass = someVar, ... )
– lko
Sep 11 '12 at 7:58



data: JSON.stringify(obj)


data: JSON.stringify(obj)


var myObj = title: "x", subclass = someVar, ...





Note that this is the solution provided you actually need to send JSON (which you might with asp.net web services). In other cases it might be easier to just remove the contentType and let jQuery pass the form-encoded data.
– GSerg
Oct 17 '17 at 7:30


contentType



As noted by jitter, the $.ajax function serializes any object/array used as the data parameter into a url-encoded format. Oddly enough, the dataType parameter only applies to the response from the server - and not to any data in the request.


$.ajax


data


dataType



After encountering the same problem I downloaded and used the jquery-json plugin to correctly encode the request data to the ScriptService. Then, used the $.toJSON function to encode the desired arguments to send to the server:


$.toJSON


$.ajax(
type: "POST",
url: "EditUserProfile.aspx/DeleteRecord",
data: $.toJSON(obj),
contentType: "application/json; charset=utf-8",
dataType: "json"
....
);





thanks for pointing out that the data parameter is ignored by the call.
– Noel Abrahams
Sep 8 '11 at 16:19


data





This works, but changing data: JSON.stringify(obj) to data: JSON.stringify(obj) worked for me if your javascript class is of the style var myObj = title: "x", subclass = someVar, ... thanks to the point about the data encoding.
– lko
Sep 11 '12 at 7:57



data: JSON.stringify(obj)


data: JSON.stringify(obj)


var myObj = title: "x", subclass = someVar, ...



it's working
something like this


data: JSON.stringify('id':x),





This answer turned up in the low quality review queue, presumably because you don't provide any explanation of the code. If this code answers the question, consider adding adding some text explaining the code in your answer. This way, you are far more likely to get more upvotes — and help the questioner learn something new.
– lmo
Sep 7 '16 at 22:19





I want to pass two parameters: an array of complex object and an integer. I do it: data: items: JSON.stringify(myarray), myId:value.
– A.Dara
Mar 1 '17 at 9:07




Jquery Ajax will default send the data as query string parameters form like:


RecordId=456&UserId=123



unless the processData option is set to false, in which case it will sent as object to the server.


processData



contentType option is for the server that in which format client has sent the data.


contentType



dataType option is for the server which tells that what type of data
client is expecting back from the server.


dataType



Don't specify contentType so that server will parse them as query
String parameters not as json.



OR



Use contentType as 'application/json; charset=utf-8' and use JSON.stringify(object) so that server would be able to deserialize json from string.



I guess @jitter was right in his guess, but his solution didn't work for me.



Here is what it did work:


$.ajax(
...
data: " intFoo: " + intFoo + " ",
...
);



I haven't tried but i think if the parametter is a string it should be like this:


$.ajax(
...
data: " intFoo: " + intFoo + ", strBar: '" + strBar + "' ",
...
);





If I had to write code like that (string concat) to create JSON objects, I would kill myself (figuratively speaking). There has to be a better way.
– PandaWood
Feb 20 '13 at 23:32



I was facing the same issue, what i came with good solution is as below:



Try this...


$.ajax(
type: "POST",
url: "EditUserProfile.aspx/DeleteRecord",
data: 'RecordId: ' + RecordId + ', UserId: ' + UId + ', UserProfileId:' + UserProfileId + ', ItemType: '' + ItemType + '', FileName: '' + XmlName + ''',
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function(msg)
if (msg.d != null)
RefreshData(ItemType, msg.d);

,
error: function(XMLHttpRequest, textStatus, errorThrown)
alert("error occured during deleting");

);



Please note here for string type parameter i have used (') escape sequence character for denoting it as string value.



If manually formatting JSON, there is a very handy validator here: jsonlint.com



Use double quotes instead of single quotes:



'project': 'a2ab6ef4-1a8c-40cd-b561-2112b6baffd6',
'franchise': '110bcca5-cc74-416a-9e2a-f90a8c5f63a0'



"project": "a2ab6ef4-1a8c-40cd-b561-2112b6baffd6",
"franchise": "18e899f6-dd71-41b7-8c45-5dc0919679ef"



On the Server, to Serialize/Deserialize json to custom objects:


public static string Serialize<T>(T obj)

DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms, obj);
string retVal = Encoding.UTF8.GetString(ms.ToArray());
return retVal;


public static T Deserialize<T>(string json)

T obj = Activator.CreateInstance<T>();
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
obj = (T)serializer.ReadObject(ms);
ms.Close();
return obj;



these answers just had me bouncing back and forth between invalid parameter and missing parameter.



this worked for me , just wrap string variables in quotes...


data: RecordId: RecordId,
UserId: UId,
UserProfileId: UserProfileId,
ItemType: '"' + ItemType + '"',
FileName: '"' + XmlName + '"'






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard