请求的资源上没有“Access-Control-Allow-Origin"标头-AngularJS

2022-01-15 00:00:00 cors angularjs ajax cross-domain
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>

和controller form对应的js是:

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);
        });
    }
}]);

当我从 URL 栏中点击它时,网络服务工作正常.如何解决问题?请帮忙!

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

推荐答案

Chrome Webstore 有一个扩展,当存在异步时会为您添加Access-Control-Allow-Origin"标头调用试图访问与您不同的主机的页面.

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.

扩展名是:Allow-Control-Allow-Origin: *",这是链接:https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi

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

相关文章