2013-03-17 21 views
1

Ajax GET Angular调用失败。使用机器IP访问本地主机会导致Ajax调用失败

它提出了以下错误:

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 

}); 
+0

您正在使用哪种浏览器?我记得用Chrome我有这个问题,如果我没有错,它与Firefox的工作。无论如何,我使用一些像Appfog这样的管理域来执行这种请求。 – steo 2013-03-17 19:21:02

+0

你必须要求一个相同的主机+端口(也是方案,例如http或https)。 – Gael 2013-03-17 19:21:29

+0

为什么它在相同的请求中显示127.0.0.1和localhost。是他们中的任何一个硬编码? – Anoop 2013-03-17 19:24:59

回答

0

看你的要求,我们可以请参阅以下内容:

Request URL:http://localhost:3000/learning_record_service/get_document?document_id=288 
... 
Host:localhost:3000 
Origin:http://127.0.0.1:3000 
Referer:http://127.0.0.1:3000/documents/open/288?sc=c961d72844bbc5439923e097860057d0&angular=1 

正如你所看到的东西正在导致你的代码混合localhost:3000127.0.0.1:3000。这被解释为默认情况下大多数浏览器不允许的“跨域”或“跨域”请求。

+0

是的,我已经看到了这个,但奇怪的是,什么是混合它。梅纳斯我在角度调用中使用相关的网址,并仍落入本地主机:3000。 :( – Gull 2013-03-17 20:57:26

相关问题