如何angularjs 1.0.8加载一个跨域JSON以$ HTTP,因为它与1.2.0不 [英] How to load a cross-domain JSON with $http in angularjs 1.0.8 as it does with 1.2.0
问题描述
采用了棱角分明1.0.8,我试图从服务器加载 JSON 了不支持get的选项,因此得到了一个错误。
Using Angular 1.0.8 I was trying to load a JSON from a server that doesn't support get OPTIONS, and consequently got a error.
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.
我试图更新的角度来1.2.0-RC和错误消失。
I've tried to update angular to 1.2.0-RC and the error is gone.
这是完全一样的code,加载角新版本的一个分支,实际上code几乎是相同的,你可以的 $ HTTP angularjs文档。
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.
如何使用v1.0.8解决这个问题?
我读过有关从申请去除头,但不能使在样本code以上这项工作。
I've read some hints about removing the headers from the request but couldn't make this work over the sample code above.
推荐答案
试试这个:
delete $http.defaults.headers.common['X-Requested-With'];
我补充该行的调用$ HTTP之前它在你的1.0.8 plunker工作。
I added that line before the call to $http and it worked in your 1.0.8 plunker.
这篇关于如何angularjs 1.0.8加载一个跨域JSON以$ HTTP,因为它与1.2.0不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!