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

People picker Control in PowerApps

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

Upload attachment to SharePoint list item using Microsoft Flow

Modern page provisioning using page template

HTML field & Date Time formatting in powerapps

Step-By-Step Azure AD App Registration

Approval and auto escalation with time out in Microsoft Flow

Create and configure custom connectors for PowerApps and MSFlow from AzureFunctions

Developing custom reusable components in PowerApps