A common exception when make an ajax call and get back the value as a JSON object. “The HTTP Header ACCEPT is missing or its value is invalid”. Let us see a solution for this.
The piece of code which I tried to make an ajax call is as follows.
function executeQuery(queryTerms)
{
Results = {
element: '',
url: '',
init: function (element) {
Results.element = element;
Results.url = _spPageContextInfo.webAbsoluteUrl + "/_api/search/query?querytext='" + queryTerms + "'";
},
load: function () {
$.ajax({
url:Results.url,
method: "GET",
headers: { "ACCEPT": "application/json " },
success: Results.onSuccess,
error : Results.onError
});
},
onError: function (error) {
alert(JSON.stringify(error));
},
onSuccess: function (data) {
//Do whatever you want
}
}
Results.init($("resultsDiv"));
Results.load();
}
When I execute I get the above mentioned error. The screen shot is as follows.
The solution is very simple. By appending the HEADER as below will resolve this exception.
headers: { "ACCEPT": "application/json;odata:verbose " },
Happy Coding.
Sathish Nadarajan.
Leave a comment