如何在 angularjs 1.0.8 中使用 $http 加载跨域 JSON,就像在 1.2.0 中一样
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18905912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to load a cross-domain JSON with $http in angularjs 1.0.8 as it does with 1.2.0
提问by a--m
Using Angular 1.0.8 I was trying to load a JSONfrom a server that doesn't support get OPTIONS, and consequently got a error.
使用 Angular 1.0.8,我试图从不支持 get OPTIONS 的服务器加载JSON,因此出现错误。
OPTIONS http://www.json-generator.com/j/cdnueRTRmG 405 (Method Not Allowed)
OPTIONS http://www.json-generator.com/j/cdnueRTRmG Origin http://run.plnkr.co is not allowed by Access-Control-Allow-Origin.
XMLHttpRequest cannot load http://www.json-generator.com/j/cdnueRTRmG. Origin http://run.plnkr.co is not allowed by Access-Control-Allow-Origin.
I've tried to update angular to 1.2.0-RC and the error is gone.
我尝试将 angular 更新为 1.2.0-RC,但错误消失了。
This is exactly the same code, a fork that loads the new version of angular, in fact the code is almost the same you can find in $http angularjs docs.
这是完全相同的代码,一个加载新版本 angular 的 fork,实际上代码几乎与您在$http angularjs docs 中可以找到的相同。
How can I fix the this problem using the v1.0.8?
如何使用 v1.0.8 解决此问题?
I've read some hints about removing the headers from the requestbut couldn't make this work over the sample code above.
我已经阅读了一些关于从请求中删除标头的提示,但无法通过上面的示例代码完成这项工作。
采纳答案by aet
Try this:
尝试这个:
delete $http.defaults.headers.common['X-Requested-With'];
I added that line before the call to $http and it worked in your 1.0.8 plunker.
我在调用 $http 之前添加了该行,它在您的 1.0.8 plunker 中工作。

