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);
          });        
    }
1
  • MERGE returns only Status Code like 200 if it is success. May be response.json() is causing the error. Jul 31, 2017 at 7:34

2 Answers 2

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.

3
  • Yes, it is returning status: "204" and statusText: "No Content", i confirmed. Jul 31, 2017 at 9:04
  • I worked as per your suggestion and I got the solution. Thanks! Jul 31, 2017 at 9:30
  • Welcome! Now you can accept my answer Jul 31, 2017 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.");
      });
}
5
  • Does this solves your problem? Jul 31, 2017 at 9:19
  • 1
    @AakashMorya Yes. It's working absolutely fine. Jul 31, 2017 at 9:26
  • How it is different than my answer??? Jul 31, 2017 at 12:56
  • @Atish Dipongkor It's not different. I worked on it as per your suggestion. Should I remove my answer post.? Jul 31, 2017 at 13:41
  • yes. you should as it is not different Aug 1, 2017 at 5:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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