【问题标题】:Angular $resource calling error function on success call to server成功调用服务器时的 Angular $resource 调用错误函数
【发布时间】:2015-04-28 10:23:32
【问题描述】:

Angular $resource 在调用成功时总是调用错误回调。我已通过 Fiddler 验证服务器返回的 json 成功代码为 200。请记住,这是在 Asp.net MVC 视图中发生的。 restful 调用的返回值是单个对象。

//Setup
$scope.targetResource = $resource(baseUrl + ":target", { target:    @target" });

// Triggering event
$scope.handleEvent = function() {
    $scope.targetResource.get({ target: "somevalue" },
        function() {
            // Never raises this function
        },
        function (responseError)
        {
            // Raises this function everytime with this response
            // {
            //    "data": null,
            //    "status": 0,
            //    "config": {
            //        "method": "GET",
            //        "transformRequest": [null],
            //        "transformResponse": [null],
            //        "url": "(the correct url)",
            //        "headers": {
            //            "Accept":"application/json, text/plain, */*"
            //        }
            //    },
            //    "statusText":""
            //}
        }
    });

我的设置中是否缺少某些内容?它正在服务器上调用正确的方法并传递正确的值。

【问题讨论】:

    标签: javascript angularjs model-view-controller resources ngresource


    【解决方案1】:

    在您显示的示例响应中,状态为 0。Angular 不会将状态代码 0 解释为“成功”。

    来自https://docs.angularjs.org/api/ng/service/$http

    200 到 299 之间的响应状态代码被视为成功 状态并将导致成功回调被调用。

    所以在您的服务器端代码中返回状态码 200,而不是 0。

    【讨论】:

      【解决方案2】:

      我发现问题在于我不正确地进行了跨域调用。不使用 JSONP 的 MVC 项目到 WebApi 项目。我将调用移至源服务器,它现在可以正常工作。谢谢彼得的帮助!

      【讨论】:

        猜你喜欢
        • 2015-01-10
        • 1970-01-01
        • 2013-07-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-03
        • 2016-11-10
        • 2019-03-30
        • 2015-02-08
        相关资源
        最近更新 更多