CORS请求不得与AngularJS [英] CORS request is not allowed with AngularJS

查看:116
本文介绍了CORS请求不得与AngularJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用下面的code访问从角度JS控制器的API:

I'm trying to access an API from an Angular JS controller using the following code:

$scope.getOperators = function() {
    $scope.CSVData.forEach(function(entry) {
        $http.defaults.useXDomain = true;
        $http({
            url : 'http://api.pts.se/PTSNumberService/Pts_Number_Service.svc/json/SearchByNumberList?numbers=' + number,
            method : 'get'
        }).success(function(data, status, headers, config) {
            console.log(data);
        });
    });
};

不过,我不断收到错误:

But I keep getting the error:

XMLHttpRequest cannot load http://api.pts.se/PTSNumberService/Pts_Number_Service.svc/json/SearchByNumberList?numbers=76-3095686. Origin http://localhost:8888 is not allowed by Access-Control-Allow-Origin.

从我确实了解,我知道CORS请求应该从服务器上启用,但此刻的请求从客户端发送的。

From what I did understand, I know that CORS request should be enabled from the server, but at the moment the request is sent from the client side.

我缺少的东西吗?

推荐答案

服务器 proivdes CORS支持。你需要启用它。

The server proivdes CORS support. You'll need to enable it there.

Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Origin: *

在理想情况下,你会替换访问控制允许来源只是域要允许,否则任何人都可以从自己的服务器进行远程/访问API。

Ideally you'd replace the Access-Control-Allow-Origin to just domains that you want to allow or else anyone could access the api remotely / from their servers.

这篇关于CORS请求不得与AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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