我有searce了很多关于AJAX调用CORS的解决方案,但我仍然无法从其他服务器获取XML数据。
I have searce a lot of solutions about AJAX call CORS, but I still can not get XML data from that other server.
这是控制台注:
XMLHtt prequest无法加载url.xml。没有访问控制 - 允许 - 原产地标头的请求的资源present。原产地的http://本地主机,因此不允许访问
XMLHttpRequest cannot load url.xml. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access
我已经按照指示来自: http://www.html5rocks.com/en/tutorials / CORS / 部分:从jQuery的CORS,这是code,我尝试:
I have follow instructions from: http://www.html5rocks.com/en/tutorials/cors/ section: CORS from jQuery, this is code that I try:
$.ajax({
type:'GET',
url:'http://www.someurl.xml',
contentType:'text/plain',
xhrFields:{
withCredentials: false
},
headers: {
'Access-Control-Allow-Origin':'http://localhost:8080',
'Access-Control-Allow-Method':'GET',
'Access-Control-Allow-Headers':'Content-Type,x-requested-with,Authorization,Access-Control-Allow-Origin'
},
success: function(data){
var test = data;
}
});
我知道这个问题已经被问了很多次,但没有答案帮助解决我的问题。
I know that this question has been asked for many times, but no answer help to fix my problem.
有关测试在本地主机我使用IIS 8.5
For testing in localhost I am using IIS 8.5
在 $。阿贾克斯 code将报头到请求的到的服务器,但CORS头必须是present上反应的在的服务器。
The headers
section in your $.ajax
code adds headers to the request to the server, but CORS headers need to be present on responses from the server.
在IIS中,您可以在℃的几行添加这些报头/ code>。加入这将让你开始为GET请求:
Working with IIS, you can add those headers with a few lines in the <system.webServer>
section of your web.config
. Adding this will get you started for GET requests:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
请参阅这篇文章以获取更多信息: HTTP: //encosia.com/using-cors-to-access-asp-net-services-across-domains/
See this post for more information: http://encosia.com/using-cors-to-access-asp-net-services-across-domains/