Forum Discussion
Calling _api/search/postquery from Modern Web Part (security validation invalid)
I am trying to call /_api/_search/postquery from a modern Script Editor web part, and I am getting this error:
VM6497:86 {"error":{"code":"-2130575251, 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."}}}
This same code works fine in classic sharepoint web part, is there something additional I have to account for in a simple REST API call in modern?
var postData = {
'request':{
'__metadata' : {'type' : 'Microsoft.Office.Server.Search.REST.SearchRequest'},
'Querytext' : "*",
}};
$.ajax({
type: "POST",
url : "https://mytenant.sharepoint.com/_api/search/postquery",
data: JSON.stringify(postData),
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose"
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success : function onSuccess(data, request){
console.log(data);
},
error : function onError(error) {
console.log(error.responseText);
}
});I've found that if I make POSTs from a different site (or site collection) than the REST URL target, my on page RequestDigest won't do. I need to grab a fresh one from the target site _api/contextinfo.
Not sure if this is your issue, but worth a try.
3 Replies
- Brent EllisSilver ContributorWell, magically, I come in today, and now this is working as it should. Not sure why it wouldn't work yesterday.
- Richard BourkeIron Contributor
I've found that if I make POSTs from a different site (or site collection) than the REST URL target, my on page RequestDigest won't do. I need to grab a fresh one from the target site _api/contextinfo.
Not sure if this is your issue, but worth a try.
- Brent EllisSilver ContributorAttempting that currently with no luck :(
The RequestDigest from the _api/contextinfo and the POST are on the same site, and I get the same error.