无“访问控制允许来源”标头所请求的资源 - AngularJS present [英] No 'Access-Control-Allow-Origin' header is present on the requested resource- AngularJS

查看:310
本文介绍了无“访问控制允许来源”标头所请求的资源 - AngularJS present的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

当我试图从我的code内运行我的Web服务我得到这个错误。我试图找到它,并试图其中有人建议我净发现了很多解决方案。粘贴低于code。

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>

和控制器形成相应的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);
        });
    }
}]);

该网站的服务工作正常,当我打从地址栏。如何解决这个问题呢?请帮助!

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

推荐答案

在客户端,您可以启用CORS在AngularJS请求

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

不过,如果这仍然返回一个错误,这将意味着你正在请求的服务器必须允许CORS要求,并已被配置为。

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.

这篇关于无“访问控制允许来源”标头所请求的资源 - AngularJS present的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆