CORS不起作用

时间:2013-06-04 09:22:18

标签: ajax xml jquery-mobile cross-domain cors

我需要编写一个ajax请求,它将从REST服务请求XML。我在本地部署了一个简单的REST服务,它返回一个XML文档。

我知道相同的原始策略,因此我将 Access-control-allow-origin:* 添加到响应中(或者在请求中的Origin头中添加了相同的值)。然后我从本地html文件测试了我的ajax请求,并得到了同样的原始策略错误。

然后我尝试将此页面放入java Web应用程序,将其部署到tomcat中并得到了同样的错误。我已经在Chrome和Firefox中测试了它。

仅当我启动具有禁用网络安全功能的Chrome时,它才有效。但它并没有解决我的问题。

这是请求,由ajax发送,接收

**OPTIONS http://localhost:9198/helloworld Origin http://localhost:8081 is not allowed by Access-Control-Allow-Origin.**

对于这两种情况。是否为 Access-control-allow-origin 中的*或已定义的主机。

Request URL:http://localhost:9198/helloworld
Request Method:OPTIONS
Status Code:200 OK

Request Headers:

Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4
Access-Control-Request-Headers:accept, customheader, origin
Access-Control-Request-Method:GET
Connection:keep-alive
Host:localhost:9198
Origin:http://localhost:8081
Referer:http://localhost:8081/testRest/test.html
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36

Response Headers:

Allow:OPTIONS,GET,HEAD
Content-length:564
Content-type:application/vnd.sun.wadl+xml
Date:Tue, 04 Jun 2013 08:48:28 GMT
Last-modified:B, 04 8N= 2013 20:23:45 MAGST

如果我只是将REST服务的地址放入浏览器,那么这是请求\响应。

Request URL:http://localhost:9198/helloworld
Request Method:GET
Status Code:200 OK

Request Headers:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:max-age=0
Connection:keep-alive
Cookie:srmuser=kbar
Host:localhost:9198
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36

Response Headers:

Access-control-allow-origin:http://localhost:8081
Content-type:application/xml
Date:Tue, 04 Jun 2013 09:10:03 GMT
Transfer-encoding:chunked

有人可以帮助我吗?

0 个答案:

没有答案