角$ HTTP错误响应状态文本总是不确定 [英] Angular $http error response statusText always undefined

查看:158
本文介绍了角$ HTTP错误响应状态文本总是不确定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图返回一个自定义错误信息给用户,让他们知道哪里出了问题,如果发生错误,但是我已经试过各种显示信息并没有什么似乎捕捉它。这里是我的角度code:

I am trying to return a custom error message to the user to let them know what went wrong if an error occurs, but I have tried everything to display the message and nothing seems to be capturing it. Here is my angular code:

$scope.save = function (style) {
    styleAPIservice.addStyle(style).success(function () {
        $scope.success = "Style added successfully";
    }).error(function (data, status, headers, config, statusText) {
        $scope.error = "An error occurred while saving style: " + data + "|"+data.data+"|"+data.statusText+"|"+statusText;
    });
}

下面是在 styleAPIservice 功能它调用:

styleAPI.addStyle = function (style) {
    return $http.post(AppRoot + "api/StyleAPI/",
        JSON.stringify(style),
            {
                headers: {
                    'Content-Type': 'application/json'
                }
            });
}

这是在 API 功能:

[HttpPost]
public HttpResponseMessage PostStyle(Style style)
{
    if (string.IsNullOrEmpty(style.Pattern.PatternName) || string.IsNullOrEmpty(style.StockNumber))
        return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Pattern Name and Stock Number are required.");

    var checkStyle = StyleRepository.LoadStyleByStockNumber(style.StockNumber);
    if (checkStyle != null)
        return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Style number already exists. Please use update.");

    try
    {
        // Save style
    }
    catch (Exception ex)
    {
        return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Error creating style: " + ex.Message);
    }

    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, style);
    response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = style.StyleId }));
    return response;
}

这是发生错误时是什么返回(如风格已经存在):

And here is what is returned when an error occurs (such as Style already exists):

An error occurred while saving style: [object Object]|undefined|undefined|undefined

我是什么做错了吗?我觉得我已经到处搜寻,并试图每一个可能的建议,但我不知所措,为什么我不能显示我的错误信息是正义。

What am I doing wrong? I feel like I've searched everywhere and tried every possible suggestion, but I am just at a loss as to why I can't display my error messages.

推荐答案

有关其他人谁可能有这个问题,该消息在 data.Message 。我发现它由调试JS。

For anyone else who may have had this issue, the message was in data.Message. I found it by debugging the JS.

这篇关于角$ HTTP错误响应状态文本总是不确定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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