Sunday, March 22, 2015

HTTP header ACCEPT is missing or its value is invalid – solution

Going from Beta to RTM, some things changed in the way that we retrieve values using REST in the SharePoint 2013 client object model.

Description & Solution

In the older versions of the object model, we could simply use something like this in your REST call:
1
2
3
4
5
6
7
8
9
10
11
12
$.ajax(
    {
        url: SPSearchResults.url,
        method: "GET",
        headers:
        {
                "accept": "application/json",
        },
        success: SPSearchResults.onSuccess,
        error: SPSearchResults.onError
    }
);
As we can see the "headers" is specifying "application/json".
The fix is simply to swap that statement into this:
1
2
3
4
5
6
7
8
9
10
11
12
$.ajax(
    {
        url: SPSearchResults.url,
        method: "GET",
        headers:
        {
                "accept": "application/json;odata=verbose",
        },
        success: SPSearchResults.onSuccess,
        error: SPSearchResults.onError
    }
);
And that’s a wrap.

Summary

I hope this can save someone a few minutes (or more) of debugging for using old example code or ripping up older projects. 

No comments:

Post a Comment