【发布时间】:2012-10-01 21:24:49
【问题描述】:
我正在尝试使用 Angular.js 服务从我使用 Jersey/Spring 制作的 REST 服务中获取 JSON。
当我使用 firefox 时,它工作正常,但是当我转到 Chrome 时,我收到了消息:
XMLHttpRequest cannot load http://localhost:8080/VirtualLawyerPj/services/timeoftheday/asjson/tue. Origin http://localhost:8090 is not allowed by Access-Control-Allow-Origin.
这是我的服务代码:
angular.module('myAppServices', ['ngResource']).
factory('Time', function($resource){
return $resource('http://localhost:port/VirtualLawyerPj/services/timeoftheday/asjson/tue',{port:":8080"}, {
query: {method:'GET', params:{}, isArray:false}
});
});
有人遇到过同样的问题吗? 你是怎么做到的?
谢谢。
【问题讨论】:
-
从命令行启动 chrome 并添加
--disable-web-security标志。/path/to/chrome/chrome --disable-web-security。非常适合测试 -
它适用于测试!但是生产呢?我正在开始一个新项目,对 Angular.js 中的这个“问题”感到有点伤痕累累
-
不是 angular.js 的问题,而是普通浏览器和 XMLHttpRequest 的问题。它应该在生产中工作,因为你不会在本地主机上。但是我对CORS了解不多,你得等别人。
-
在最坏的情况下,您始终可以在服务器上代理请求。
-
见stackoverflow.com/questions/18053547/… => Chrome.exe --allow-file-access-from-files
标签: angularjs