0

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?

3
  • thats you servers job to fix that. Commented Oct 13, 2014 at 17:30
  • the resource on there is going to have to send the right headers down. Commented Oct 13, 2014 at 17:32
  • You are missing the "Access-Control-Allow-Origin" header on the server side. Take a look here : blog.bulte.net/12-24-2013/angular-wordpress-cors.html Commented Oct 13, 2014 at 17:39

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Problem is that the server's not mine. The server is api.pts.se and probably they haven't enabled CORS.
Well in that case you can't perform requests. You'll have to proxy through PHP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.