2016-11-24 58 views
2

當使用HTML Web資源加載Dynamics CRM表單時,我從Chrome瀏覽器控制檯收到以下錯誤消息。在Web資源上動態CRM Web API身份驗證錯誤

的https:‌ //xxxx.api.crm6.dynamics.com/api/data/v8.2/ <自定義實體>。請求的資源上沒有「Access-Control-Allow-Origin」標題。原因'https://.crm6.dynamics.com'因此不被允許訪問。響應有HTTP狀態代碼401

<script type="text/javascript"> 

var clientUrl = "https://xxxx.api.crm6.dynamics.com/api/data/v8.2/" 

function GetAccounts() { 
    var url = clientUrl + "accounts" 

    $.ajax({ 
     method: "GET", 
     url: url, 
     async: false, 
     beforeSend: getAccountsBeforeSendCallback, 
     fail: getAccountsFailCallback, 
     done: getSavingGoalsDoneCallback, 
     success: getAccountsSuccessCallback 
    }); 
} 

function getAccountsBeforeSendCallback(jqXHR, settings) { 
    debugger 
    jqXHR.setRequestHeader("OData-MaxVersion", "4.0"); 
    jqXHR.setRequestHeader("OData-Version", "4.0"); 
    jqXHR.setRequestHeader("Accept", "application/json"); 
    jqXHR.setRequestHeader("Content-Type", "application/json; charset=utf-8"); 
} 
</script> 
+0

大多數討論的主題都是一般的。 CORS和JSONP將做到這一點。 https://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource –

回答

1

看來你正在做的另一個域的請求。你確定你的clientUrl在同一個域上嗎?

var clientUrl = "https://xxxx.api.crm6.dynamics.com/api/data/v8.2/"; 
var rightUrl = window.Xrm.Page.context.getClientUrl() + "/api/data/v8.2"; 
if (clientUrl !== rightUrl) { 
    console.log("You will get the 'Access-Control-Allow-Origin' error!"); 
} 

很多人都遇到了$ .ajax和XmlHttpRequest的問題。幸運的是,有圖書館,這將照顧這一點。 crm-sdk的示例,它將與您的代碼完全相同:

<script type="text/javascript" src="CRMSDK.js"></script> 
<script type="text/javascript"> 
    var WebAPI = window.CRMSDK.WebAPI; 
    WebAPI.retrieveMultiple("account").then(function (data) { 
     getAccountsSuccessCallback(data); //this is your method. 
    }); 
</script>