Вопрос

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);
          });        
    }
Это было полезно?

Решение

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.

Другие советы

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.");
      });
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с sharepoint.stackexchange
scroll top