Convert variable that contain string data into JSON Object for JavaScript REST API
-
21-12-2019 - |
Question
Here I have variable named item and contain following data:
{
__metadata: {
"type": "SP.Data.ClinicalTestingPhaseIIListItem"
},
Control: 1,
Randomization: 2,
Blindings: 3,
Groups: 4,
DoseTitration: 5,
Stratification: 6,
PatientsDoseLevel: 7,
TumourResponse: 8,
DetermineWarrants: 9,
Title: 1
}
Now when I pass it to my ajax call written below that gives me a error after some research I get conclusion that the data passed in string format instead of object.
Microsoft.SharePoint.Client.InvalidClientQueryException
: A node of typePrimitiveValue
was read from the JSON reader when trying to read the start of an entry. AStartObject
node was expected.
Below is my ajax call
jQuery.ajax({
url: requestUri,
type: "POST",
data: JSON.stringify(item),
headers: header,
success:function(){
alert("List itme Inserted Sucessfully");
},
error: function(data){
console.log(data);
alert(data.responseText);
alert("Error Occured");
}
});
Solution
Remove JSON.stringify and see
jQuery.ajax({
url: requestUri,
type: "POST",
data: item,
headers: header,
success:function(){
alert("List itme Inserted Sucessfully");
},
error: function(data){
console.log(data);
alert(data.responseText);
alert("Error Occured");
}
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow