1

Ajax GET call in Angular is failing.

It raises the following error:

XMLHttpRequest cannot load http://localhost:3000/learning_record_service/get_document?document_id=288. Origin http://127.0.0.1:3000 is not allowed by Access-Control-Allow-Origin. 

And here are the request details:

Request URL:http://localhost:3000/learning_record_service/get_document?document_id=288
Request Method:OPTIONS
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:accept, origin, x-requested-with
Access-Control-Request-Method:GET
Connection:keep-alive
Host:localhost:3000
Origin:http://127.0.0.1:3000
Referer:http://127.0.0.1:3000/documents/open/288?sc=c961d72844bbc5439923e097860057d0&angular=1
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.22 (

While here is the code:

$http.get('/learning_record_service/get_document?document_id=' + document_id).success(function(data){

//success code

});
Rune FS
  • 21,497
  • 7
  • 62
  • 96
Gul
  • 1,757
  • 2
  • 15
  • 26
  • which browser are you using? I remember that with Chrome I had this problem, if I am not wrong it worked with Firefox. Anyway I use to perform this kind of request using some provvisory domain like Appfog. – steo Mar 17 '13 at 19:21
  • you have to make requests one the same host+port (also scheme, eg http or https). – Gaël Barbin Mar 17 '13 at 19:21
  • Why it showing 127.0.0.1 and localhost in same request. is any of them hard coded? – Anoop Mar 17 '13 at 19:24
  • Your web framework needs to implement CORS support: http://en.wikipedia.org/wiki/Cross-origin_resource_sharing – Jason Mar 17 '13 at 19:36
  • i m using chrome ... and accessing from ip as need te have some testing from mobile. It works fine when access from localhost but when access from 127.0.0.1:3000 it fails. – Gul Mar 17 '13 at 19:46
  • so the interesting point that i am unable to understand is that the ajax call use to adopt the base url by default why its been using localhost in each case. – Gul Mar 17 '13 at 19:49
  • possible duplicate of [localhost :: cross domain ajax](http://stackoverflow.com/questions/3849200/localhost-cross-domain-ajax) – Bergi Mar 17 '13 at 20:38

1 Answers1

0

Looking at your request we can see the following:

Request URL:http://localhost:3000/learning_record_service/get_document?document_id=288
...
Host:localhost:3000
Origin:http://127.0.0.1:3000
Referer:http://127.0.0.1:3000/documents/open/288?sc=c961d72844bbc5439923e097860057d0&angular=1

As you can see something is causing your code to mix up localhost:3000 and 127.0.0.1:3000. This is interpreted as a 'cross-origin' or 'cross-domain' request which is not allowed by most browsers by default.

Korijn
  • 1,383
  • 9
  • 27
  • yes i have seen this but the strange thing is that what is mixing it up. Menas i am using relevant url in angular call and still it falls to localhost:3000. :( – Gul Mar 17 '13 at 20:57