I need to do insert operation to SharePoint list. Below is my code..
$.ajax({
url: appWebUrl + "/_api/SP.AppContextSite(@target)" + "/web/lists/getbytitle('NewList')/items?" + "@target='" + hostWebUrl + "'",
type: "POST",
data: JSON.stringify({ '__metadata': { 'type': 'SP.Data.NewListListItem' },
'Title': "TitleVal", 'Name':"Satya"}),
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"content-Type": "application/json;odata=verbose" },
success: function (data) {
success(data);
},
error: function (xhr, status, error) {
console.log( " xhr.responseText: " + xhr.responseText + " //status: " + status + " //Error: "+error );
alert("error");
}
});
It throws error:
Microsoft.SharePoint.SPException","message":{"lang":"en-US","value":"The security validation for this page is invalid and might be corrupted. Please use your web browser's Back button to try your operation again."
POST
(or PUT) instead ofGET
if you want to do write operations.