如何启用CORS进行简单的AJAX Web应用程序 [英] How to enable CORS for simple AJAX web application

查看:146
本文介绍了如何启用CORS进行简单的AJAX Web应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有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/

这篇关于如何启用CORS进行简单的AJAX Web应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆