首页 > 解决方案 > Connecting to Cross Domain Dynamics CRM Web API (Unauthorized)

问题描述

I'm having a problem connecting to the Dynamics CRM Web API.

I'm trying to connect to the Web API from an external .NET Core page, using javascript but I keep getting the 401 Unauthorized error. It seems a cross domain problem. The error is:

No 'Access-Control-Allow-Origin' header is present on the requested resource

Basically I'm at the domain domain1.com, and I need to run a web api call at domain2.com using javascript.

So how can I achieve that? I do not want my user to be prompted to authenticate, but I need to manage that by the code.

Also, I'm not using Azure to authenticate but I'm using ADFS.

This is my code:

$.ajax({
    type: "GET",
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    url: "https://crm.anuv.com.br/api/data/v8.2/EntityDefinitions(LogicalName='account')",
    beforeSend: function (XMLHttpRequest) {
        XMLHttpRequest.setRequestHeader("OData-MaxVersion", "4.0");
        XMLHttpRequest.setRequestHeader("OData-Version", "4.0");
        XMLHttpRequest.setRequestHeader("Accept", "application/json");
        XMLHttpRequest.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    },
    async: false,
    success: function (data, textStatus, xhr) {
        var results = data;
        var jsonResult = JSON.parse(data);
        alert(jsonResult.Attributes.length);
    },
    error: function (xhr, textStatus, errorThrown) {
        alert(errorThrown);
    }
});

标签: jqueryasp.net-web-apidynamics-crmmicrosoft-dynamicsdynamics-crm-webapi

解决方案


推荐阅读