0

我需要编写一个 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

有人可以帮我吗?

4

0 回答 0