4

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.

a--m
  • 4,716
  • 1
  • 39
  • 59

1 Answers1

1

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.

aet
  • 7,192
  • 3
  • 27
  • 25
  • 2
    The json-generator stuff backing that plunkr probably expired or was removed. The technique is still valid. Thanks for the downvote! – aet Nov 05 '13 at 17:09
  • 1
    you should remove the plunkr link as it does not exist anymore. – unludo Nov 21 '13 at 16:10
  • @aet I'm having this same issue (client / api communication works when hosted on same server, but not cross domain) and this method causes a different error: Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.3.2/$injector/modulerr?p0=testApp&p1=TypeErro…%20at%20Gd%20(http%3A%2F%2Flocalhost%3A5001%2Fjs%2Fangular.min.js%3A17%3A1) – Michael Merchant Dec 12 '14 at 22:07