为什么POST响应为204(无内容)会触发Angularjs错误函数? [英] Why does a POST response of 204 (no content) trigger the Angularjs error function?

查看:85
本文介绍了为什么POST响应为204(无内容)会触发Angularjs错误函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用$http.post()进行Web服务调用的Angularjs应用程序.它调用的方法具有void返回类型,并生成204(无内容)响应代码.这将进入.then()回调中定义的错误函数,并在日志中显示未找到元素".

I have an Angularjs application that uses $http.post() to make web service calls. The method it calls has a void return type, and it generates a 204 (no content) response code. This goes into the error function that is defined in the .then() callback, and prints "no element found" in the log.

如果我从方法中返回任何内容,它将返回正常的200响应代码.

If I return anything from the method, it returns the normal 200 response code.

为什么成功代码会触发错误功能?

根据要求输入代码:

function myFn(inputData) {
    var d = { data: inputData };
    $http.post("../api/my/serverFn", d)
        .then(
            function(response) {
                $rootScope.AddMsg("Success");
            },
            function(response) {
                 // The 204 response goes here.
                 $rootScope.AddMsg("Error");
            });
}

要求的屏幕截图:

推荐答案

AngularJS本身并不将204响应视为错误.调查显示,实际上,如果收到的响应状态为204,则堆栈中会有一个HTTP拦截器拒绝了诺言,从而将成功的响应转换为错误.

AngularJS doesn't natively consider a 204 response as an error. The investigation revealed that there was in fact an http interceptor in the stack that rejected the promise if the response received had a 204 status, turning the successful response into an error.

这篇关于为什么POST响应为204(无内容)会触发Angularjs错误函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆