0

I'm new to AngularJS, so please, bear with me. I got the following code:

var testModule = angular.module("testModule", ['ngResource']);

testModule.config(function($sceDelegateProvider) {
    $sceDelegateProvider.resourceUrlWhitelist([
    'self',
    'https://api.twitter.com/**']);
}); 

testModule.controller("SecondController",
    function SecondController($scope, $resource){
        $scope.secondField = "Hello World";
        try{
            var restClient = 
                //$resource("https://api.github.com/repos/angular/angular.js/issues", [],
                $resource("https://api.twitter.com/1.1/search/tweets.json", [],
                    {
                        "get":{method:"GET", headers:{"Accept":"application/json"},
                            interceptor: {
                                "response":function(data){
                                    alert("Response: " + data);
                                },
                                "responseError":function(data){
                                    alert("Error: " + data);
                                }
                            }
                        }
                    });
            $scope.about = restClient.get();
        } catch(err) {
            $scope.error = err.message;
        }
        $scope.done = true;
    }
);

It's supposed to execute the responseError function and data.status should be equal to 215 (the request requires authentication to pass successfully). Instead, the response code is 0.

I'm actually trying to reach a different API (that does not require authentication), but the status code is also 0 and I don't really have any idea what the issue might be. The only successful request I could execute till now is:

GET https://api.github.com/repos/angular/angular.js/issues

Executed by $resource.query(). But nor twitter, nor the target API can be reached. I hope providing me a solution for the twitter example can help me solve my issue.

Thanks.

Edit: Using AngularJS 1.2.0 rc2

1 Answer 1

1

This may be because of cross domain request. Status code 0 will come when you try to access web service of different domain. In this case you need to use JSONP as method in ngResource.

http://docs.angularjs.org/api/ngResource.$resource
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.