Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

XMLHttpRequest cannot load http://mywebservice. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access. 

I get this error when I try to run my web-service from inside my code. I tried finding about it and tried many solutions which were suggested which I found on net. Pasting the code below.

<form name="LoginForm" ng-controller="LoginCtrl" ng-submit="init(username,password,country)">     <label>Country</label><input type="text" ng-model="country"/><br/><br/>     <label>UserName</label><input type="text" ng-model="username" /></br></br>     <label>Password</label><input type="password" ng-model="password">     </br>     <button type="submit" >Login</button> </form> 

And controller form the corresponding js is:

app.controller('LoginController', ['$http', '$scope', function ($scope, $http) {     $scope.login = function (credentials) {     $http.get('http://mywebservice').success(function ( data ) {         alert(data);         });     } }]); 

The web-service works fine when I hit it from URL bar. How to resolve the problem? Kindly help!

like image 725
Ru11 Avatar asked Jun 10 '14 06:06

Ru11


People also ask

How do I fix CORS header Access-Control allow Origin missing?

If the server is under your control, add the origin of the requesting site to the set of domains permitted access by adding it to the Access-Control-Allow-Origin header's value. You can also configure a site to allow any site to access it by using the * wildcard. You should only use this for public APIs.

How do you solve CORS issue in angular 8?

CORS error due to browser's same origin policy. To get around this, you need to tell your browser to enable your client and your server to share resources while being of different origins. In other words, you need to enable cross-origin resource sharing or CORS in your application.

Why does CORS policy no Access-Control allow origin?

Why was the CORS error there in the first place? The error stems from a security mechanism that browsers implement called the same-origin policy. The same-origin policy fights one of the most common cyber attacks out there: cross-site request forgery.


Video Answer


2 Answers

The Chrome Webstore has an extension that adds the 'Access-Control-Allow-Origin' header for you when there is an asynchronous call in the page that tries to access a different host than yours.

The name of the extension is: "Allow-Control-Allow-Origin: *" and this is the link: https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi

like image 89
Patricio Córdova Avatar answered Sep 22 '22 22:09

Patricio Córdova


On the client side you can enable cors requests in AngularJS via

app.config(['$httpProvider', function($httpProvider) {         $httpProvider.defaults.useXDomain = true;         delete $httpProvider.defaults.headers.common['X-Requested-With'];     } ]); 

However if this still returns an error, this would imply that the server that you are making the request has to allow CORS request and has to be configured for that.

like image 25
ALi Mir Avatar answered Sep 18 '22 22:09

ALi Mir