为 MSCRM Web API (Cross-Sub-Domain) 在 Chrome 和 FF 中启用 CORS
Enabling CORS in Chrome & FF for MSCRM Web API (Cross-Sub-Domain)
我正在开发一个应用程序,该应用程序将托管在与我们的 MSCRM 安装不同的 sub-domain 上。在此应用程序中,我依靠 CRM 的 WebAPI 来完成多项任务,例如检索和记录创建。在 Internet Explorer 中一切正常,但我在 FireFox 和 Chrome 中继续遇到讨厌的错误。到目前为止,我已经尝试了几种不同的方法,包括纯 XHR、JQuery/Ajax、Iframe 和 Jsonp(pre-built 和手动)。我确实确保在 MSCRM 服务器的 Web 配置中包含 CORS header。继续注意这已经在 Internet Explorer 中工作也很重要。
我从 chrome 收到的错误是:
XMLHttpRequest cannot load https://mycrm.mydomain/myorg/api/data/v8.0/new_entityname(00000000-0000-0000-0000-000000000000)?$select=new_fieldname,. Request header field Prefer is not allowed by Access-Control-Allow-Headers in preflight response.
在 FireFox 中,我没有收到任何消息。根本行不通。
这是我目前正在使用的功能。
var Request = function (query, successCallback, errorCallback) {
var _url = FormatString("{0}/{1}/api/data/v8.0/{2}", CrmConnection.BaseUrl, CrmConnection._org, query);
var req = new XMLHttpRequest();
req.open("GET", _url, true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);
if (results["value"] || null != null)
successCallback(results.value);
else
successCallback(results);
}
else {
console.log("Begin Response Headers"); //
console.log(this.getAllResponseHeaders()); // Returns empty string
console.log("End Response Headers"); //
errorCallback(FormatString("XHRWAPI Error: '{0}';", this.statusText)); // The end result of this message is XHRWAPI Error: '' <-- It is also an empty string
}
}
};
req.send();
}
这些是我添加到 MSCRM 安装的 web.config。
的 header
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS" />
</customHeaders>
</httpProtocol>
即使我能指出正确的方向,也非常感谢您的帮助。
正如 Jaromanda X 所说,添加缺失的 headers 是解决方案。只需将 headers 添加到您的配置文件或适用的 IIS。
我正在开发一个应用程序,该应用程序将托管在与我们的 MSCRM 安装不同的 sub-domain 上。在此应用程序中,我依靠 CRM 的 WebAPI 来完成多项任务,例如检索和记录创建。在 Internet Explorer 中一切正常,但我在 FireFox 和 Chrome 中继续遇到讨厌的错误。到目前为止,我已经尝试了几种不同的方法,包括纯 XHR、JQuery/Ajax、Iframe 和 Jsonp(pre-built 和手动)。我确实确保在 MSCRM 服务器的 Web 配置中包含 CORS header。继续注意这已经在 Internet Explorer 中工作也很重要。
我从 chrome 收到的错误是:
XMLHttpRequest cannot load https://mycrm.mydomain/myorg/api/data/v8.0/new_entityname(00000000-0000-0000-0000-000000000000)?$select=new_fieldname,. Request header field Prefer is not allowed by Access-Control-Allow-Headers in preflight response.
在 FireFox 中,我没有收到任何消息。根本行不通。
这是我目前正在使用的功能。
var Request = function (query, successCallback, errorCallback) {
var _url = FormatString("{0}/{1}/api/data/v8.0/{2}", CrmConnection.BaseUrl, CrmConnection._org, query);
var req = new XMLHttpRequest();
req.open("GET", _url, true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);
if (results["value"] || null != null)
successCallback(results.value);
else
successCallback(results);
}
else {
console.log("Begin Response Headers"); //
console.log(this.getAllResponseHeaders()); // Returns empty string
console.log("End Response Headers"); //
errorCallback(FormatString("XHRWAPI Error: '{0}';", this.statusText)); // The end result of this message is XHRWAPI Error: '' <-- It is also an empty string
}
}
};
req.send();
}
这些是我添加到 MSCRM 安装的 web.config。
的 header<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS" />
</customHeaders>
</httpProtocol>
即使我能指出正确的方向,也非常感谢您的帮助。
正如 Jaromanda X 所说,添加缺失的 headers 是解决方案。只需将 headers 添加到您的配置文件或适用的 IIS。