【问题标题】:AngularJS: Status code 0 when executing GET request using ngResourceAngularJS:使用 ngResource 执行 GET 请求时的状态码 0
【发布时间】:2013-10-10 15:47:52
【问题描述】:

我是 AngularJS 的新手,所以请多多包涵。我得到以下代码:

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;
    }
);

应该执行responseError函数,data.status应该等于215(请求需要认证才能成功通过)。相反,响应代码是 0。

我实际上是在尝试使用不同的 API(不需要身份验证),但状态码也是 0,我真的不知道问题可能是什么。到目前为止,我唯一可以执行的成功请求是:

获取https://api.github.com/repos/angular/angular.js/issues

由 $resource.query() 执行。但也无法访问 twitter,也无法访问目标 API。我希望为我提供 twitter 示例的解决方案可以帮助我解决我的问题。

谢谢。

编辑:使用 AngularJS 1.2.0 rc2

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    这可能是因为跨域请求。当您尝试访问不同域的 Web 服务时,会出现状态码 0。在这种情况下,您需要在 ngResource 中使用 JSONP 作为方法。

    http://docs.angularjs.org/api/ngResource.$resource
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-03
      • 2014-10-19
      • 2021-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-15
      • 2023-01-02
      相关资源
      最近更新 更多