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

Comments

  1. THANK YOU so much. I spent almost a week trying to figure out how to fix the sometime '#document' return value!

    ReplyDelete
  2. The 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)

    https://github.com/SharePoint/sp-dev-docs/issues/1892

    ReplyDelete

Post a Comment

Enter your comments..

Popular posts from this blog

Secure When a HTTP request is received Power Automate a.k.a MS Flow

People picker Control in PowerApps

Upload attachment to SharePoint list item using Microsoft Flow

Modern page provisioning using page template

Headless-Daemon calling AAD secured API

Approval and auto escalation with time out in Microsoft Flow

Developing custom reusable components in PowerApps

Step-By-Step Azure AD App Registration

Create and configure custom connectors for PowerApps and MSFlow from AzureFunctions

HTML field & Date Time formatting in powerapps