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

No #39;Access-Control-Allow-Origin#39; header is present on the requested resource- AngularJS(请求的资源上没有“Access-Control-Allow-Origin标头-AngularJS)
本文介绍了请求的资源上没有“Access-Control-Allow-Origin"标头-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>

和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

这篇关于请求的资源上没有“Access-Control-Allow-Origin"标头-AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Show a progress bar for downloading files using XHR2/AJAX(显示使用 XHR2/AJAX 下载文件的进度条)
How do I get the HTTP status code with jQuery?(如何使用 jQuery 获取 HTTP 状态码?)
How to get response url in XMLHttpRequest?(如何在 XMLHttpRequest 中获取响应 url?)
WebKit quot;Refused to set unsafe header #39;content-length#39;quot;(WebKit “拒绝设置不安全的标头‘内容长度’)
$.ajax call working fine in IE8 and Doesn#39;t work in firefox and chrome browsers($.ajax 调用在 IE8 中运行良好,但在 Firefox 和 chrome 浏览器中不起作用)
How do I know if jQuery has an Ajax request pending?(我如何知道 jQuery 是否有待处理的 Ajax 请求?)