有没有办法将额外的字段传递给jqXHR对象?这是我的情况:
我正在返回一个HttpResponseMessage这样的:
response.ReasonPhrase = "someString " + Resources.Messages.NoData;
response.StatusCode = HttpStatusCode.NoContent;
return response;
但在某些情况下StatusCode = HttpStatusCode.NoContent;,出于不同的原因,我需要通过另一个:
response.ReasonPhrase = "anotherString " + Resources.Messages.NoMoreData;
response.StatusCode = HttpStatusCode.NoContent;
return response;
因为消息都被本地化(Resources.Messages)到用户语言,我需要一个额外的字段来检查例如它是否等于"someString "或"anotherString "或“....”在AJAX success回调中:
$.ajax({
type: 'POST',
url: '/api/testing/test',
data: jobject,
contentType: 'application/json',
success: function (data, status, xhr) {
if (status == "nocontent" && xhr.statusText.startsWith("someString")) {
// do something
}
if (status == "nocontent" && xhr.statusText.startsWith("anotherString")) {
// do something
}
error: function (xhr) {
debugger;
if (xhr.responseJSON == 'UnableToParseDateTime') {
// do something
}
}
});
奇怪的是,xhr.statusText它不支持xhr.statusText.startsWith(). "someString "所以对or或 "...."的相等检查"anotherString "不起作用。
我注意到responseText在回叫中总是空的success。如果我可以从服务器“填充”那一个会很好。如果没有,我怎么能有一个额外的jqXHR对象字段?或者定义几个新的自定义HttpStatusCode?