Chrome上的CORS的XMLHttpRequest问题

时间:2017-04-12 14:54:48

标签: javascript web-services cors

我是这个概念的新手,需要帮助来解决这个问题。我有从一个域到另一个域的webservice调用。它适用于IE,但在Chrome / Safari和Firefox上中断。根据网上提供的指南,我更新了用于CORS访问的web.config,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="<url of domain from where request is made" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>

在javascript中我正在制作xmlhttprequest,如下所示:

xmlhttp=new XMLHttpRequest();

xmlhttp.onreadystatechange = function () {
// Processing done based on response}
if ("withCredentials" in xmlhttp) 
{
xmlhttp.open('GET',url +'/Xsearch?text=value&treepath=/People/&type=xml',true);
} else if (typeof XDomainRequest != "undefined") {
xmlhttp = new XDomainRequest();
xmlhttp.open('GET', url + '/Xsearch?text=value&treepath=/People/&type=xml');
} else {
xmlhttp = null;

}

var encodeusernamepassword=btoa(<usernmae>+<password>);

xmlhttp.setRequestHeader("Authorization", "Basic " + encodeusernamepassword);

xmlhttp.setRequestHeader("Access-Control-Allow-Origin",<url from where request is made>);

xmlhttp.setRequestHeader("Access-Control-Allow-Credentials","true");

xmlhttp.withCredentials = true;

xmlhttp.send();

我不断收到一个或另一个错误。请帮助我如何使其适用于所有浏览器。

0 个答案:

没有答案