1

I am developing a webapp using AngularJS and a REST API based on Django-Tastypie to get data.

To be able to perform ajax requests, I enabled Cross Origin Resource Sharing on django using django-cors-header framework (https://github.com/ottoyiu/django-cors-headers), but I noticed in chrome inspector that the preflighted request (an OPTIONS request) that is sent before the main request is cancelled almost immediately and I saw in the Django server logs this : "OPTIONS /api/airport/19643 HTTP/1.1" 301

I found some posts about this suggesting to delete a the 'X-Requested-With' parameter from the header ($http.get is not allowed by Access-Control-Allow-Origin but $.ajax is) but this resolves the problem only for GET requests. So if I try to send a PUT or POST request, a preflighted request is sent and gets cancelled !

Here is the what I see on the inspector when I enable CORS but don't delete the previous parameter from the header: enter image description here

I get the same error for PUT requests as well.

I can't really see where the problem is, so I hope if someone could point me to the right direction.

Thanks a lot

Community
  • 1
  • 1
Anas
  • 437
  • 6
  • 19
  • if your server supports JSONP then use JSONP. – Abhishek Nandi Aug 07 '13 at 13:01
  • I was using it before and it works well, but in my application, I need to send PUT requests as well to update some informations in the database based on client parameters... and unfortunately JSONP doesn't support this – Anas Aug 07 '13 at 13:05
  • 1
    you can use a proxy router or you can refer to the solution suggested on git regarding this.. will try to find the link and share – Abhishek Nandi Aug 07 '13 at 16:08

2 Answers2

0

I suggest,Can you try below command line options for chrome for allowing CORS in local testing :

Chrome.exe --allow-file-access-from-files

Note : Your chrome must not be open. When you run this command chrome will open automatically.

If you are entering this command in command prompt then select your chrome installation directory then use this command.

JQuery Guru
  • 6,943
  • 1
  • 33
  • 40
  • I think it's not the problem I'm facing ! Here I have two different domains : an API runs on the first one and the Angular app on the second one. And it's about performing ajax requests between those domains to get data from the API and use it in angular. The problem I'm encountering then is how to perform cross site requests even if I am configuring my Data server to accept all requests !! Hope this make my problem clear :) – Anas Aug 07 '13 at 13:12
0

Well the problem was a missing / in the URL. So instead of sending an ajax request to : /api/airport/19643, I should've used /api/airport/19643/ (weird...)

(for more details about CORS, I recommand you to see https://developers.google.com/storage/docs/cross-origin )

Anas
  • 437
  • 6
  • 19