I am trying to do POST
operations such update, create and delete items using rest and javascript. I am getting 402
Error mostly attributed this to having a wrong/corrupt requestdigest
value.
When I tried out an ajax call to http://<site url>/_api/contextinfo
- I get the error 405 - invalid method
Code for Creating new Item -
function addNewItem(no)
{
jQuery.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/GetByTitle('TestWF')/Items",
type: "POST",
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
"content-Type": "application/json;odata=verbose"
},
data: JSON.stringify({
_metadata: { 'type': 'SP.Data.TestWFListItem' },
Title: 'Test'
}),
success: function(data) {
console.log('ghhjgk');
},
error: function(error) {
jQuery('#status').text( error.responseText)
}
});
}
error :
-2130575251, Microsoft.SharePoint.SPExceptionThe security validation for this page is invalid and might be corrupted. Please use your web browser's Back button to try your operation again.
Code for refreshing requestdigest
function updateReqDigest() {
jQuery.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/contextinfo",
method: "POST",
headers: { "Accept": "application/json; odata=verbose"},
success: function (data) {
console.log(data)
},
error: function (data, errorCode, errorMessage) {
jQuery('#status').text( data.responseText); }
});
}
Error :
-1, Microsoft.SharePoint.Client.ClientServiceExceptionThe HTTP method 'GET' cannot be used to access the resource 'GetContextWebInformation'. The operation type of the resource is specified as 'Default'. Please use correct HTTP method to invoke the resource.
I have also tried this :
var r = UpdateFormDigest(_spPageContextInfo.webServerRelativeUrl, _spFormDigestRefreshInterval);
alert(r);
No Error is thrown in this case, and r
is undefined. Also, the value of _spFormDigestRefreshInterval
is 14400
Is this the default value ?
Update
According to @Shekar's Answer, I replaced ajax Call with call through SP.RequestExecutor
When, I try to Create a new item in the list using RequestExecutor
the success callback gets executed but the item doesnt get created.
When I try to update an existing item in RequestExecutor
, I get an error -
"A supported MIME type could not be found that matches the content type of the response. None of the supported type(s) 'application/atom+xml;type=entry, application/atom+xml, application/json;odata=verbose' matches the content type 'application/json'."
_metdata
to__metadata
, still the issue persists.