Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Web Service Error 401

Posted on by Microsoft Employee

Hi, i m trying to use the web service of the crm on a form.But it's returning this error

XMLHttpRequest cannot load x.api.crm4.dynamics.com/.../accounts.
 No 'Access-Control-Allow-Origin' header is present on the requested resource.
 Origin 'https://x.crm4.dynamics.com' is therefore not allowed access. The response had HTTP status code 401.

On crm documentation say that is not necessary nothing because OAuth.BTW i 'm assign with admin credentials and if a put the path into url is show data.

function GetExistingNIF(nifValue)
{

    var Paramet = "accounts";
    //modificar para o link do crm de produção ->Settings > Customizações > link do service root url
    var clientURL = "x.api.crm4.dynamics.com";

    var req = new XMLHttpRequest()
    req.open("POST", encodeURI(clientURL + "/api/data/v8.2/" + Paramet), true);
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");

    req.onreadystatechange = function ()
    {
        debugger;
        if (this.readyState == 4)
        {
            req.onreadystatechange = null;
            if (this.status == 204)
            {
                var accountUri = this.getResponseHeader("OData-EntityId");
                console.log("Created account with URI: " + accountUri);
            }
            else
            {
                var error = JSON.parse(this.response).error;
                console.log(error.message);
            }
        }
    };
    req.send(JSON.stringify({ name: "Sample account" }));
}


I try use xhr and assign withCredentials to true but still not working.It's any system option need to be enable??

Thanks in advance

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Web Service Error 401

    I'm trying to make this call on webresource js on a form.i read this msdn.microsoft.com/.../gg334279.aspx and it's  says  "When you use JavaScript web resources in Microsoft Dynamics 365 you do not need to authenticate because the web resources are part of the application the user is already authenticated. "

  • Verified answer
    Gopalan Bhuvanesh Profile Picture
    Gopalan Bhuvanesh 11,397 on at
    RE: Web Service Error 401

    Hi

    This is a cross domain issue.

    If cross domain, please refer the following thread.

    community.dynamics.com/.../668271

    If you are using the current url then use:

    var clientURL = Xrm.Page.context.getClientUrl();

  • Suggested answer
    ThomasN Profile Picture
    ThomasN 3,190 on at
    RE: Web Service Error 401

    Hi H0nestly, thanks for reaching out.

    This sounds like a CORS (cross origin) issue. That error is occurring because the authentication method does not support CORS.

    It looks like you are using the Web API which is the only supported method. The next steps are to authorize "Implicit Flow" on you application or website performing this call to the WEB API. It would depend on how you are handling credentials, are the users logged into your website and authorized to CRM through your Active Directory?

    Below is a link which is a good place to start. Let me know how we can help.

    msdn.microsoft.com/.../mt595799.aspx

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans