REST call returns XML instead of JSON in SharePoint
Hi All,
enforce REST call to return JSON format instead of #document(XML).
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/currentuser",
type: "GET",
contentType : "application/json",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
console.log(data);
custUser = data.d;
},
error: function (error) {
alert(JSON.stringify(error));
}
});
we would get the problematic output like below:
add the these lines in "beforeSend" method
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/currentuser",
type: "GET",
contentType : "application/json",
headers: { "Accept": "application/json; odata=verbose" },
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("Accept", "application/json; odata=verbose");
},
success: function (data) {
console.log(data);
custUser = data.d;
},
error: function (error) {
alert(JSON.stringify(error));
}
});
after adding the header in beforeSend method the output will like below
:) coding
Regards
Ratsub
enforce REST call to return JSON format instead of #document(XML).
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/currentuser",
type: "GET",
contentType : "application/json",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
console.log(data);
custUser = data.d;
},
error: function (error) {
alert(JSON.stringify(error));
}
});
we would get the problematic output like below:
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/currentuser",
type: "GET",
contentType : "application/json",
headers: { "Accept": "application/json; odata=verbose" },
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("Accept", "application/json; odata=verbose");
},
success: function (data) {
console.log(data);
custUser = data.d;
},
error: function (error) {
alert(JSON.stringify(error));
}
});
after adding the header in beforeSend method the output will like below
:) coding
Regards
Ratsub
THANK YOU so much. I spent almost a week trying to figure out how to fix the sometime '#document' return value!
ReplyDeleteThe above suggestion didn't worked for me, clearing browser cache using alt + ctrl + delete or chrome "clear cache and hard reload" fixes the issue for me. (ctrl + f5 or ctrl + shift + r didn't worked)
ReplyDeletehttps://github.com/SharePoint/sp-dev-docs/issues/1892