In this article we will discuss about SharePoint Form Digest issue in REST API. At the time of POST event in SharePoint REST API, we have to pass "X-RequestDigest": jQuery("#__REQUESTDIGEST").val(); but it seems, some time jQuery("#__REQUESTDIGEST").val(); returns noting(undefined).
So, in this case we can use below code to get Form Digest and can use in our JavaScript code instead of jQuery("#__REQUESTDIGEST").val();
var hostUrl = "http://mycompany.sharepoint.com/sites/site1/SharePoint/oAuth";
function getFormDigest() {
$.support.cors = true;
var _formDigest;
$.ajax({
url: hostUrl + "/_api/contextinfo",
type: "POST",
dataType: "json",
headers: { "Accept": "application/json; odata=verbose" },
xhrFields: { withCredentials: true },
async: false,
success: function (data) {
_formDigest = data.d.GetContextWebInformation.FormDigestValue;
},
error: function () {
console.log("Error Occured");
}
});
return _formDigest;
}
Thanks for reading the article. Hope this Article will helpful for you. Cheers!!!
So, in this case we can use below code to get Form Digest and can use in our JavaScript code instead of jQuery("#__REQUESTDIGEST").val();
var hostUrl = "http://mycompany.sharepoint.com/sites/site1/SharePoint/oAuth";
function getFormDigest() {
$.support.cors = true;
var _formDigest;
$.ajax({
url: hostUrl + "/_api/contextinfo",
type: "POST",
dataType: "json",
headers: { "Accept": "application/json; odata=verbose" },
xhrFields: { withCredentials: true },
async: false,
success: function (data) {
_formDigest = data.d.GetContextWebInformation.FormDigestValue;
},
error: function () {
console.log("Error Occured");
}
});
return _formDigest;
}
Thanks for reading the article. Hope this Article will helpful for you. Cheers!!!