1

I need to get JSON data from a server with URL :

http://xxxx.xxxx.xxxx.xxxx:8084/inpulse/api/user/listall

Here's my angular code:

var app = angular.module('app',[]);

app.controller('Controller', function ($scope,$http)  {
    $scope.email="";
    $scope.password="";
    $scope.sample="";

    $http({
        method: 'GET',
        url: 'http://xxxx.xxxx.xxxx.xxxx/inpulse/api/user/listall',
    })
    .success(function (data, status, headers, config) {
        var ret = data;
        $scope.sample = JSON.stringify(ret);
    })
    .error(function (data, status, headers, config) {
        alert(status);
        // something went wrong :(
    });
});

Now the same code worked when i used a JSON test URL like http://ip.jsontest.com/. Its Definitely not a problem with the server because I tested it with a REST client and got a response. I can't figure out what I'm doing wrong.

2
  • 2
    Can you please console.log(data) in the error callback and tell us what is shows ? Commented May 27, 2014 at 11:15
  • Are you calling it from a page in the me domain ? Commented May 27, 2014 at 11:46

1 Answer 1

2

It might be a CORS configuration issue.
If you are trying to access the server from a page that is not in the same domain/origin, you'll get an error because the server is not configured to allow CORS.

The error reported by Chrome looks like this:

XMLHttpRequest cannot load http://xxxx.xxxx.xxxx.xxxx/inpulse/api/user/listall. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fiddle.jshell.net' is therefore not allowed access.

Other than that the code seems to work just as expected.

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

6 Comments

You simply can't guess that from nothing :)
That just proves the code works as expected. CORS should be configured on the server-side, not on the client-side. @Aperçu: Yes, I can ! That's why they call it "guessing". And no, it's not a good practice, but it was too long to be a comment, it is simple to verify and if it turns out not to be a problem, it can always be deleted :)
Yeah I understand your point, I was just waiting for OP to add more info about his problem to write down an answer
@Aperçu: Yeah, me too. But then I got impatient. I started writing the possible answer and waited for the OP to give more info before posting it. And then I got impatient again :)
Yeah I got the same error when I logged it. Looks like I'll need to ask for some server side changes! Thanks for the help!
|

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.