samedi 25 avril 2015

[AngularJS] No 'Access-Control-Allow-Origin' header is present on the requested resource

If your Ionic application use $http.post (for calling a restful service for example) and if you try test your application (executed with the command ionic serve) on your PC with a browser like Chrome, you will obtain this kind of message in your JavaScript console :
No 'Access-Control-Allow-Origin' header is present on the requested resource[...]

Solution :

During your test, you can do that :

  • replace :
$http.post(url,params);

  • by :
var config = {
     method: 'POST',
     data:params,
     headers: {
        'Content-Type': undefined
     },
     url=url
};$http(config);


  • and define in your restful application :
"Access-Control-Allow-Origin": "*"

Source : http://forum.ionicframework.com/t/angularjs-post-request-cors-issue/10334

Other interesting sources :
http://juhap.iki.fi/webdev/cross-domain-http-with-python/
http://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/
http://stackoverflow.com/questions/12630231/how-do-cors-and-access-control-allow-headers-work

Aucun commentaire:

Categories