2

I am updating a SharePoint list item in SharePoint framework web part, with the below code my item is getting updated, but it gives me the following error at the end :

 Unexpected end of JSON input
private updateProperties(listname:string,requestdata:{})
    {   
       let requestdatastr = JSON.stringify(requestdata);
        requestdatastr = requestdatastr.substring(1, requestdatastr .length-1);

        let requestlistItem: string = JSON.stringify({
          '__metadata': {'type': this.getListItemType(listname)}
        });

        requestlistItem = requestlistItem.substring(1, requestlistItem .length-1);
        requestlistItem = '{' + requestlistItem + ',' + requestdatastr + '}';
       //Everthing is alright till here for sure.
        this.context.spHttpClient.post(`${this.context.pageContext.web.absoluteUrl}/_api/web/lists/getbytitle('${listname}')/items`,
            SPHttpClient.configurations.v1,
            {
              headers: {
                "IF-MATCH": "*",
                "X-HTTP-Method":"MERGE",
                'Accept': 'application/json;odata=nometadata',
                'Content-type': 'application/json;odata=verbose',
                'odata-version': ''
              },
              body: requestlistItem
            })
            .then((response: SPHttpClientResponse): Promise<IListItem> => {
            return response.json();
          })
          .then((item: IListItem): void => {
              this.usermessage(`List Item created successfully... '(Item Id: ${item.Id})`);
             this._renderListAsync();  

        }, (error: any): void => { //This message prints me the error
            this.usermessage('List Item Updatation Error...');
            console.log("Error in updation"+error.message);
          });        
    }
  • MERGE returns only Status Code like 200 if it is success. May be response.json() is causing the error. – Atish Dipongkor - MVP Jul 31 '17 at 7:34
1

If you make any MERGE or PATCH request to update an item in SharePoint list, then it returns 204 HTTP status code if the request is successful. There is no content in response. Therefore, it is the reason of getting following error.

Unexpected end of JSON input

I believe you are getting the error in the following line:

return response.json();

Thus, my suggestion is that you should do something different in above line.

  • Yes, it is returning status: "204" and statusText: "No Content", i confirmed. – Dilip Kumar Yadav Jul 31 '17 at 9:04
  • I worked as per your suggestion and I got the solution. Thanks! – Dilip Kumar Yadav Jul 31 '17 at 9:30
  • Welcome! Now you can accept my answer – Atish Dipongkor - MVP Jul 31 '17 at 10:30
0

I have removed this operation :

 .then((response: SPHttpClientResponse): Promise<IListItem> => {
      return response.json();
  })

and updated the code as follows:

private updateProperties(listname:string,requestdata:{})
{  
   let requestdatastr = JSON.stringify(requestdata);
    requestdatastr = requestdatastr.substring(1, requestdatastr .length-1);

    let requestlistItem: string = JSON.stringify({
      '__metadata': {'type': this.getListItemType(listname)}
    });

    requestlistItem = requestlistItem.substring(1, requestlistItem .length-1);
    requestlistItem = '{' + requestlistItem + ',' + requestdatastr + '}';
    console.log("Unique ID in Rest:"+this.appUniqueID);

    this.context.spHttpClient.post(`${this.context.pageContext.web.absoluteUrl}/_api/web/lists/getbytitle('${listname}')/items(113)`,
    SPHttpClient.configurations.v1,
    {
      headers: {
        "IF-MATCH": "*",
        "X-HTTP-Method":"MERGE",
        'Accept': 'application/json;odata=nometadata',
        'Content-type': 'application/json;odata=verbose',
        'odata-version': ''
      },
      body: requestlistItem
    })
    .then((response: SPHttpClientResponse): void => {
        alert("Update Success.");
      }, (error: any): void => {
       alert("Update Fail.");
      });
}
  • Does this solves your problem? – Aakash Maurya Jul 31 '17 at 9:19
  • 1
    @AakashMorya Yes. It's working absolutely fine. – Dilip Kumar Yadav Jul 31 '17 at 9:26
  • How it is different than my answer??? – Atish Dipongkor - MVP Jul 31 '17 at 12:56
  • @Atish Dipongkor It's not different. I worked on it as per your suggestion. Should I remove my answer post.? – Dilip Kumar Yadav Jul 31 '17 at 13:41
  • yes. you should as it is not different – Atish Dipongkor - MVP Aug 1 '17 at 5:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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