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

Sathish Nadarajan
 
Solution Architect
May 26, 2014
 
Rate this article
 
Views
17726

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.

Category : SharePoint

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment