1

My first time with http request. I use Angular for it. Server works normal - it's public news API. I need to get JSON file by URL like "hostname.com/article/2014/06/10/123?api-key=1234567890".

function Ctrl($scope, $http, $templateCache) {
    //some code there
    $scope.load_article = function( patch ) {
        $http.get(patch + "?" + $scope.apikey)
            .success(function(response){
                result = angular.fromJson(response.data);
                $scope.article =  result;
            }).error(function(response) {
                $scope.article = "error "+ response.status;
        });
    };
}

But when i call load_article() tracer shows me that result:

Method: OPTIONS;
Status: 596 OK;
Type: text/xml;

and "error undefined" into $scope.article.

Where is my fault?

Upd:

$http.jsonp(patch + "?" + $scope.apikey).success(function(data)){...}

Will be better for get JSON file.

8
  • 1
    The Http 596 error code usually indicates that the api endpoint you are hitting does not exist. Are you sure you're using the correct api call? Commented Jun 21, 2014 at 13:44
  • @zszep when I copy patch with api-key directly to browser it shows me correct json file. Commented Jun 21, 2014 at 13:49
  • OK, then it might be a cross domain issue. If it's working via http but not XmlHttpRequest, you may try JSONP to get the data. All modern browsers restrict cross domain call via javascript. Commented Jun 21, 2014 at 13:56
  • @zszep if I change "$http.get(patch + "?" + $scope.apikey)" on the "$http.jsonp(patch + "?" + $scope.apikey)", there is no statmen after calling load_article()... No success, no errors. Looks like that func ignored. Commented Jun 21, 2014 at 14:08
  • 1
    JSONP usually requires you to send a callback function in your request (you should look up the documentation for the api). If you'd tell us what public news API you're using, someone might help. Commented Jun 21, 2014 at 14:10

1 Answer 1

2

JSONP usually requires you to send a callback function in your request (you should look up the documentation for the api). If you'd tell us what public news API you're using, someone might help.

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

Comments

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.