简体   繁体   English

我在angularjs中缺少CORS标题'Access-Control-Allow-Origin'

[英]im getting CORS header ‘Access-Control-Allow-Origin’ missing in angularjs

I'm getting a CORS header missing error. 我收到一个CORS标头丢失错误。 I can't modify the code of the back end web service, I can only change the client side application. 我无法修改后端Web服务的代码,我只能更改客户端应用程序。

I can add the " Allow control allow origin " addon on google chrome but I don't want to install the add on all the clients to access the api. 我可以在谷歌浏览器上添加“ Allow control allow origin ”插件,但我不想在所有客户端上安装添加以访问API。 How can i change my AngularJS code so that I will not get this issue? 如何更改我的AngularJS代码以便我不会遇到此问题?

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope, $http) {
    $http.get('url', {
        headers: { 'Authorization': 'Basic a2VybmVsc3B==' }
    })
        .then(function (response) {
            $scope.names = response.data;
        });
});
</script>

As mentioned in the comments, you will have to use a proxy, or some sort of proxy. 正如所提到的意见,你将不得不使用代理,或某种代理。 There is no way around this. 没有办法解决这个问题。

However, it is fairly straightforward: make the server that serves the angularjs application do the api call. 但是,它非常简单: 使为angularjs应用程序服务的服务器执行api调用。

First, you have to understand what server and client is. 首先,您必须了解服务器和客户端是什么。 Afterwards, you need to understand that your angularjs application is served from a server. 然后,你需要了解你的应用程序angularjs从服务器提供服务 Your angularjs application can make http requests to that server, which will in turn make the call to the api, and return the result to the client: 您的angularjs应用程序可以向该服务器发出http请求,然后该服务器将调用api,并将结果返回给客户端:

粗略绘制的图表

I am somewhat assuming a Node server is serving your angularjs application, but any server can do the same, it will be able to make the http request without being a cross origin request. 我有点假设Node服务器正在为您的angularjs应用程序服务,但是任何服务器都可以这样做,它将能够发出http请求而不是跨源请求。


In your case, when you do the url call, instead, call the server that serves your application, and then, from that server, create a service that will call the external api. 在您的情况下,当您进行url调用时,请调用服务于您的应用程序的服务器,然后从该服务器创建一个将调用外部API的服务。

I had the same error. 我有同样的错误。 Angular at front makes requests to backend. 前面的Angular请求后端。 Nginx is the proxy. Nginx是代理。 So I added this line to my nginx config 所以我把这行添加到我的nginx配置中

location /database/ {
        add_header Access-Control-Allow-Origin *;
        <other_lines>
}

This solved my CORSs issue. 这解决了我的CORS问题。

if you are using IIS Web server please add below config to your web.config file. 如果您使用的是IIS Web服务器,请将以下配置添加到您的web.config文件中。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <httpProtocol>
            <customHeaders>
                <add name="Access-Control-Allow-Origin" value="*" />
            </customHeaders>
        </httpProtocol>
    </system.webServer>
</configuration>

If you dont have access to backend use $http.jsonp(url) instead of $http.get(url) 如果你没有访问后端使用$http.jsonp(url)而不是$http.get(url)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM