【发布时间】:2014-02-17 09:48:43
【问题描述】:
我希望能够从我的(自托管)web-api 返回自定义错误。
我在服务器配置中设置了以下标志...
HttpSelfHostConfiguration config = new HttpSelfHostConfiguration(baseAddress);
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
在服务器端控制器中,我有如下内容
public HttpResponseMessage Post(MyDatadata)
{
// do stuff and received an error back..
HttpError error = new HttpError("test test");
error.Add("mycode", "abcd");
error.Add("mymdetails", "my detailed message");
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, error);
}
但是,在我的 Javacript 中进行发布时,我无法解决任何这些错误。
例如,我将拥有
$.ajax({
type: 'POST',
url: 'postUri',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(data)
})
.fail(function(jqXhr, textStatus, errorThrown) {
var ttt = JSON.parse(jqXhr.responseText).message;
if (jqXhr.getResponseHeader('Content-Type').indexOf('application/json') > -1) {
// only parse the response if you know it is JSON
var error = $.parseJSON(jqXhr.responseText);
alert(error.Message);
} else {
alert('Fatal error');
}
});
但是jqXhr.responseText始终是一个空字符串,在传回.error的数据中的其他任何地方我都看不到这个字符串
当我在提琴手(原始视图)中查看响应时,我确实看到了这些字符串..
HTTP/1.1 500 Internal Server Error
Content-Length: 91
Content-Type: application/json; charset=utf-8
Server: Microsoft-HTTPAPI/2.0
Date: Sun, 09 Feb 2014 01:20:31 GMT
{
"message": "test test",
"mycode": "abcd",
"mymdetails": "my detailed message"
}
但我只是看不到如何从 Javascript 获取这些
有人知道这里出了什么问题吗?
提前感谢您提供任何信息!
【问题讨论】:
标签: javascript asp.net-web-api