Angular $http错误响应statusText总是undefined
本文关键字:statusText 总是 undefined 响应 错误 http Angular | 更新日期: 2023-09-27 18:16:10
我试图返回一个自定义错误消息给用户,让他们知道什么出错了,如果发生错误,但我已经尝试了一切来显示消息,似乎没有捕获它。下面是我的angular代码:
$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
我做错了什么?我觉得我已经到处搜索并尝试了每一个可能的建议,但我只是不知所措,为什么我不能显示我的错误信息。
替换.catch . error()的()。
$http.post('/url',json)
.success(function(data, status, headers, config){
// some code here
})
.catch(function(data, status, headers, config){ // <--- catch instead error
data.statusText; //contains the error message
});
对于任何可能遇到此问题的人,消息在data.Message
中。
StatusText只能通过使用。then从$http中获取,不能从。post helper方法中获取,也不能从。success .error中获取。
function handleSuccess(data, status, headers, config, statusText){
//some function of your own invention
}
function handleError(data, status, headers, config, statusText){
//some function of your own invention
}
//borrowed from angularJs Http module
function headersGetter(headers) {
var headersObj = isObject(headers) ? headers : undefined;
return function(name) {
if (!headersObj) headersObj = parseHeaders(headers);
if (name) {
var value = headersObj[lowercase(name)];
if (value === void 0) {
value = null;
}
return value;
}
return headersObj;
};
}
//borrowed from angularJs Http module
function isSuccess(status) {
var istatus = Math.max(status, 0);
return 200 <= istatus && istatus < 300;
}
$scope.postMyData = function ($http)
{
var req = {
method: 'POST',
url: 'destinationURL.html',
headers: {
'Content-Type': 'application/json'
},
data: $scope,
};
// example response from :
//http://plnkr.co/edit/atRTBQC62YdZzKH8mWqu?p=preview
//{
// "data":"Hello, $http!",
// "status":200,
// "config":{
// "method":"GET",
// "transformRequest":[null],
// "transformResponse":[null],
// "url":"http-hello.html",
// "cache":{},
// "headers":{"Accept":"application/json, text/plain, */*"}
// },
// "statusText":"OK"}
$http(req).
then(function (response) {
var data = response.data;
var status = response.status;
var headers = headersGetter(headers)
var config = response.config;
var statusText = response.statusText;
scope.messageToUsers = (isSuccess(response.status))
? handleSuccess(data, status, headers, config, statusText)
: handleError(data, status, headers, config, statusText);
})
}