简体   繁体   中英

CORS request is not allowed with AngularJS

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.

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.

Am I missing something?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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