0

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'."

  • Just updated the answer – Swayam Siddha Sep 11 '15 at 9:38
  • Right. Fair enough :) – Akhoy Sep 11 '15 at 9:39
  • Did you make sure you passed the correct Body data? I see only one underscore(_) in your code for _metadata. It needs to be two (__metadata) – sekhar Sep 11 '15 at 9:49
  • i updated _metdata to __metadata, still the issue persists. – Swayam Siddha Sep 11 '15 at 10:01
4

I faced similar issues with $.ajax and did it in a different way. Here is my article about making REST calls. I used OOTB SP.RequestExecutor Object in /_layouts/15/sp.requestexecutor.js. to make the post requests. It worked perfecctly in all environments.

https://sharepointatwork.wordpress.com

  • I made some progress with your answer but later found that it doesnt solve my issue. – Swayam Siddha Sep 11 '15 at 9:40
2

The issue was weirdly around conflict between multiple jQuery Files. The $ pointed to the jQuery Tools code instead of jQuery 1.11.x instead. Hence, (I dont know how but ..) all the $ POST calls got resolved to $ GET

@Sekhar has a very nifty trick, that should ideally have circumvented the $ issue but somehow it didn't. Thanks Sekhar

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.