代码之家  ›  专栏  ›  技术社区  ›  BT101

success$http call angularJS中未定义响应

  •  -1
  • BT101  · 技术社区  · 7 年前

    虽然我的后端工作正常,而且我从邮递员精心编制的请求中得到了正确的响应

    src

    我在angularJS控制器中看不到响应。(我在控制器内执行此调用以简化情况)

    $scope.click = function (auction_id) {
        $http({
            url: baseUrl + 'auctions/' + auction_id +'/followers',
            headers: {
                'Content-Type' : 'application/vnd.api+json'
            },
            method: 'POST'
        })
            .then(function(response) {
                console.log(response);
            })
            .catch(function(response) {
                return response;
            });
    };
    

    我正在使用httpInterceptor传递令牌,它对我的应用程序的其余部分工作正常。

    URL正确,因为我在控制台中获取了有效的错误号:

    帖子#####################/v1/拍卖/172/关注者

    422(不可处理实体)

    类别TRL。js:64未定义

    64行是一个控制台登录成功 .then(function... .

    邮件标题(我相信是)来自postman选项卡的响应标题(第三个来自第一个屏幕截图中的Body)

    src

    为什么响应未定义?

    *url代码中的哈希是我的。

    2 回复  |  直到 4 年前
        1
  •  0
  •   Egel    7 年前

    从REST API请求中,您得到状态为422的响应,这意味着您有一个客户端错误。关于您的请求,您必须在出现错误时处理请求。要处理异步请求中的错误,还有第二个参数 .then(mySuccessMethod(), myMethodOnError()) 方法

    有关的更多详细信息 .then() .catch() 本票的方法。

    $scope.click = function (auction_id) {
    $http({
        url: baseUrl + 'auctions/' + auction_id +'/followers',
        headers: {
            'Content-Type' : 'application/vnd.api+json'
        },
        method: 'POST'
    })
        .then(function(response) {
            console.log(response);
        }, function(error) {
            // Here goes your code to handle an error with status 4XX 
            console.log(error)
        })
        .catch(function(response) {
            // Catch will come when you throw an error
            return response;
        });
    };
    
        2
  •  0
  •   Kevin Le - Khnle    7 年前

    当您在Postman中发出请求时,您将在请求的标头的Auth属性中传递令牌。在代码中,您没有这样做。