【发布时间】:2013-03-17 19:18:19
【问题描述】:
Angular 中的 Ajax GET 调用失败。
它会引发以下错误:
XMLHttpRequest cannot load http://localhost:3000/learning_record_service/get_document?document_id=288. Origin http://127.0.0.1:3000 is not allowed by Access-Control-Allow-Origin.
这里是请求的详细信息:
Request URL:http://localhost:3000/learning_record_service/get_document?document_id=288
Request Method:OPTIONS
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:accept, origin, x-requested-with
Access-Control-Request-Method:GET
Connection:keep-alive
Host:localhost:3000
Origin:http://127.0.0.1:3000
Referer:http://127.0.0.1:3000/documents/open/288?sc=c961d72844bbc5439923e097860057d0&angular=1
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.22 (
这里是代码:
$http.get('/learning_record_service/get_document?document_id=' + document_id).success(function(data){
//success code
});
【问题讨论】:
-
您使用的是哪个浏览器?我记得使用 Chrome 时我遇到了这个问题,如果我没记错的话,它适用于 Firefox。无论如何,我曾经使用 Appfog 之类的 provvisory 域来执行这种请求。
-
您必须向一个相同的主机+端口发出请求(也是方案,例如 http 或 https)。
-
为什么它在同一个请求中显示 127.0.0.1 和 localhost。它们中的任何一个都是硬编码的吗?
-
你的 web 框架需要实现 CORS 支持:en.wikipedia.org/wiki/Cross-origin_resource_sharing
-
我正在使用 chrome ... 并根据需要从 ip 访问,您可以从移动设备进行一些测试。从 localhost 访问时它工作正常,但从 127.0.0.1:3000 访问时失败。
标签: javascript ajax angularjs