如何在 angularjs 1.0.8 中加载带有$http的跨域 JSON,就像在 1.2.0 中一样

How to load a cross-domain JSON with $http in angularjs 1.0.8 as it does with 1.2.0

本文关键字:JSON 一样 http 加载 angularjs      更新时间:2023-09-26

使用 Angular 1.0.8 我试图从不支持 get OPTIONS 的服务器加载 JSON,因此出现错误。

1.0.8 演示

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,但错误消失了。

1.2.0 演示

这是完全相同的代码,一个加载新版本 angular 的分支,实际上代码几乎与您在 angularjs 文档中$http代码相同。

如何使用 v1.0.8 解决此问题?

我已经阅读了一些有关从请求中删除标头的提示,但无法通过上面的示例代码进行此操作。

试试这个:

delete $http.defaults.headers.common['X-Requested-With'];

我在调用$http之前添加了该行,它在您的 1.0.8 plunker 中有效。