简体   繁体   中英

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

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.

Demo for 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. 

I've tried to update angular to 1.2.0-RC and the error is gone.

Demo for 1.2.0

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 .

How can I fix the this problem using the v1.0.8?

I've read some hints about removing the headers from the request but couldn't make this work over the sample code above.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM