1

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.

  • Which type column of "Image_x0020_Location" ? – Thales Chemenian Dec 5 '16 at 21:21
  • Image_x0020_Location is a "Hyperlink or Picture" – UrhoKarila Dec 5 '16 at 21:23
3

According to this post from Atish Dipongkor,

The right way to do the post from this type of column is:

JSON.stringify({
        '__metadata': { 'type': 'SP.Data.Submitted_x0020_VideosListItem' },
        'Title': this.sub.title,
        'URL0': this.sub.url,
        'Image_x0020_Location': {
             __metadata: { "type": "SP.FieldUrlValue" },
             Url: "http://test.com",
             Description: "Url Description"
        }
    }),

Made my own test, and it worked:

<d:Test m:type="SP.FieldUrlValue">
    <d:Description>Url Description</d:Description>
    <d:Url>http://test.com</d:Url>
</d:Test>
  • Thanks! That was able to get the testsubmit() working, but the generalized form is giving me more (and stranger) trouble. – UrhoKarila Dec 5 '16 at 21:37
  • Why did you change your 'URL0' column if it was working? There's no need to change if it is a FieldMultLineText. Change back to be equal as testSubmit function. – Thales Chemenian Dec 5 '16 at 21:42
  • It's working in the TestSubmit() function, but not in the Submit() function. I tried both the standard 'URL0': this.sub.url and the Multiline one, and wasn't having luck with either of them. – UrhoKarila Dec 5 '16 at 21:46
  • Update your post with your submit() function then. – Thales Chemenian Dec 5 '16 at 21:53
  • Heh, sorry, head's a little off kilter. The submit calls UploadImage() and hands off the returned image URL to UploadItem() -- that's included in the question text. – UrhoKarila Dec 5 '16 at 21:54

Your Answer

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.