【发布时间】:2015-03-04 08:47:26
【问题描述】:
我正在尝试向用户返回一条自定义错误消息,让他们知道如果发生错误时出了什么问题,但我已经尝试了所有方法来显示该消息,但似乎没有任何内容可以捕获它。这是我的角度代码:
$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;
}
这是发生错误时返回的内容(例如Style已经存在):
An error occurred while saving style: [object Object]|undefined|undefined|undefined
我做错了什么?我觉得我到处搜索并尝试了所有可能的建议,但我不知道为什么我无法显示我的错误消息。
【问题讨论】:
-
尝试调试代码以查看“数据”对象中的内容?
-
好吧,我采纳了您的建议并在 Visual Studio 中进行了调试。消息在 data.Message 中。我现在觉得很笨。在这上面浪费了这么多时间! >.
标签: c# angularjs asp.net-mvc-4