I have not been able to get Angular $http to communicate with a remote REST service. I have tried Restangular and $resource too. The problem seems to be with the underlying $http service.
I am getting the following error:
XMLHttpRequest cannot load http://www.EXAMPLE-DOMAIN.com/api/v2/users/sign_in. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.
I have researched this a lot. Currently I have tried setting the $httpProvider headings when configuring my app module and played with $http headers. Below is some of my current code.
I didnât have any luck with the settings you have in your App Config. The headers you are setting will have no affect. The headers have to be returned in the response from the server. Sometimes, running a local http server helps with this issue. I didnât have any issues running on a device or simulator(within Cordova mobile app). I only had some issues running in a desktop browser. In my case, I have control over the server and can enable CORS functionality on it. If you dont have that luxury, I have read some cases where a proxy service can be used to get around this issue. I hope that helps in some way.
I will likely be making a server update eventually. However I discovered two things that helped me solve this for my development environment.
The PhoneGap build for the app does not have the CORS issue. So no problem on actual buildsâŚ
I can turn off CORS security on my Chrome browser for testing. I only run this no-CORS browser when in dev. You can do this on OSX with the following command: open -a Google\ Chrome --args --disable-web-security I just created a little bin script for it.
I solved âAccess-Control-Allow-Originâ problem by adding CORS filter on server side. Let me know if anybody is facing this issue. I will post my code and steps.
Hope this will help anyone who stumbles onto this question in the future.
Just so I give you the âmainâ point, which is as other guys basically said (but a bit more detailed), is the Access-Control-Allow-Origin. This is my âAPIâ, in PHP:
<?php
//http://stackoverflow.com/questions/18382740/cors-not-working-php
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day
}
// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
//http://stackoverflow.com/questions/15485354/angular-http-post-to-php-and-undefined
$postdata = file_get_contents("php://input");
if (isset($postdata)) {
$request = json_decode($postdata);
$username = $request->username;
if ($username != "") {
echo "Server returns: " . $username;
}
else {
echo "Empty username parameter!";
}
}
else {
echo "Not called properly with username parameter!";
}
?>
If youâre testing locally use this chrome extension iâve been using it a while now you can also try running the app on a device and check out the debugger in chrome
An easy fix is to download the CORS plugin for your browser. Then just whitelist your API URL so any further requests you make will not be blocked because of the cross-origin issues.
Using CORS plugin on browser just help only you, others will still get the âAccess Control Allow Originâ problem. you should understand what is Access-Control-Allow-Origin and how it works first.
XMLHttpRequest cannot load http://lvh.me:3000/user_types. No âAccess-Control-Allow-Originâ header is present on the requested resource. Origin âhttp://lvh.me:9000â is therefore not allowed access. The response had HTTP status code 404.
<<< \
Hi @keithdmoore I am getting above error in windows
XMLHttpRequest cannot load http://myLink. No âAccess-Control-Allow-Originâ header is present on the requested resource. Origin âhttp://localhost:8100â is therefore not allowed access. The response had HTTP status code 401.
But i need to change only in client side i cant change in server side. can u give me any steps to do this???
Hi guys, i struggled for way too long with CORS issues caused by the interaction of an emulated Ionic project (running localy) with some small Node.JS webservices (also running localy).
Behold the short easy workaround : Chromeâs plugin AllowControlAllowOrigin.
Just switch the radio button from red to green, and no more blocked request, error or warning !
Of course itâs a temporary solution and you may have to put your hands in your request headers when the time to stop the local tests will come. In the meantime, thatâs a great way to avoid loosing time on those anoying recurrent issues.
Add flags ââuser-data-dir=âC:/ChromeDevSessionâ --disable-web-securityâ at the end of Object string.
e.g.
âC:\Program Files (x86)\Google\Chrome\Application\chrome.exeâ --user-data-dir=âC:/ChromeDevSessionâ --disable-web-security