加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

在所请求的资源AngularJS上不存在“Access-Control-Allow-Origin

发布时间:2020-12-17 09:05:08 所属栏目:安全 来源:网络整理
导读: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. 我得到这个错误,当我尝试从我的代码内部运行我的Web
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.

我得到这个错误,当我尝试从我的代码内部运行我的Web服务。我试图找到它,并尝试了许多解决方案,我建议在网上找到。粘贴下面的代码。

<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>

并且控制器形成相应的js是:

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

当我从URL栏命中web服务工作正常。如何解决问题?请帮忙!

在客户端,您可以通过AngularJS启用cors请求
app.config(['$httpProvider',function($httpProvider) {
        $httpProvider.defaults.useXDomain = true;
        delete $httpProvider.defaults.headers.common['X-Requested-With'];
    }
]);

但是,如果这仍然返回一个错误,这将暗示您正在发出请求的服务器必须允许CORS请求,并且必须为此配置。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读