The HTTP Header ACCEPT is missing or its value is invalid – SharePoint 2013


Sathish Nadarajan
SharePoint MVP
Published On :   26 May 2014
Visit Count
Today :  1    Total :   11881



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.

image

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.

Categories