When trying to POST some data to a SP list, I get an error telling me:
An unexpected 'PrimitiveValue' node was found when reading from the JSON reader. A 'StartObject' node was expected.
Interestingly, the testSubmit function works just fine without the Image_x0020_Location
line.
For example, this (full function) doesn't work and yields the aforementioned error.
this.testSubmit = function () {
jQuery.ajax({
url: this.rootUrl + this.apiUrl + this.listUrl,
type: "POST",
data: JSON.stringify({
'__metadata': { 'type': 'SP.Data.Submitted_x0020_VideosListItem' },
'Title': this.sub.title,
'URL0': this.sub.url,
'Image_x0020_Location': this.sub.imgurl,
}),
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": this.sub.formDigest
},
success: function successCallback(data) {
console.log("Test insert was a success");
console.log(data);
},
error: function errorCallback(data) {
console.log("Test insert failed");
console.log(data);
}
});
};
However, replacing data
with
data: JSON.stringify({
'__metadata': { 'type': 'SP.Data.Submitted_x0020_VideosListItem' },
'Title': this.sub.title,
'URL0': this.sub.url,
//'Image_x0020_Location': this.sub.imgurl,
}),
Works just fine, although it doesn't submit an image URL like it ought to.
Why is this, and how can I get it working?
Huh. Changing the TestSubmit method to include
data: JSON.stringify({
'__metadata': { 'type': 'SP.Data.Submitted_x0020_VideosListItem' },
Title: "Test item",
URL0: "https://google.com",
'Image_x0020_Location': { Description: "Image location", Url: "https://testing.image" }
}),
has worked like a charm. However, my more generalized method is showing some strange behaviour.
this.uploadItem = function (sub) {
jQuery.ajax({
url: this.rootUrl + this.apiUrl + this.listUrl,
type: "POST",
data: JSON.stringify({
'__metadata': { 'type': 'SP.Data.Submitted_x0020_VideosListItem' },
Title: sub.title,
URL0: sub.url,
'Image_x0020_Location': {
__metadata: { "type": "SP.FieldUrlValue" },
Description: sub.title,
Url: sub.imgurl
},
}),
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": sub.formDigest
},
success: function successCallback(data) {
console.log("Item insert was a success");
console.log(data);
},
error: function errorCallback(data) {
console.log("Item insert failed");
console.log(data);
}
Returns as success, but doesn't include the success data I'd expect. Instead of a JSON object, it gives me...
The HTML of the page that I'm on.
Item insert was a success
<html lang="en" xmlns:mso="urn:schemas-microsoft-com:office:office" xmlns:msdt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" ng-app="submissionForm">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Example</title>
<link href="./JavaScript/jcarousel.responsive.css" rel="stylesheet" />
<script type="text/javascript" src="...
what
To top it off, it's not being properly added to the list.
In the end, the strange behavior was caused by the callback structure I was using. Tightly-coupling the code let everything work as intended.
Image_x0020_Location
is a "Hyperlink or Picture"