5

I am using the SharePoint 2010 REST API, which can return data in either xml or JSON format. For my scenario I need JSON.

Everything is working fine with jQuery:

$.ajax({
     type:"GET",
     url:url,
     dataType:"json",
     success: function(data, textStatus, jqXHR){...}
   });

But I can't get JSON in plain JavaScript, the data is returned as xml. What am I missing?

var XHR=new XMLHttpRequest();
XHR.open("GET", url, true);
XHR.setRequestHeader("Content-Type","application/json");
XHR.onreadystatechange = function () {
if (XHR.readyState == 4 && XHR.status == 200) {...}};
XHR.send(null);
Christophe
  • 27,383
  • 28
  • 97
  • 140
  • Open the "Network" tab in Chrome's developer tools. Check the HTTP request headers for the jQuery request and compare them with the request headers that are sent when you do your plain Ajax-request... – Šime Vidas Jun 12 '11 at 15:40

2 Answers2

6

I believe that's a WCF oData service under the hood, which should respect the Accept header.

var XHR=new XMLHttpRequest();
XHR.open("GET", url, true);
XHR.setRequestHeader("Accept","application/json");
XHR.onreadystatechange = function () {
if (XHR.readyState == 4 && XHR.status == 200) {...}};
XHR.send(null);
Dave Ward
  • 59,815
  • 13
  • 117
  • 134
0
XHR.setRequestHeader("Content-Type","application/json");

Are you really trying to tell it that you're sending JSON to the server?

Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83