在我的asp.net mvc 4 web应用程序中,我在控制器中的操作似乎将无效数据返回到jquery ajax回调...该操作正在对IP进行ping操作,并且从几个ajax调用中多次调用它为了ping几个IP。
我在控制器中的一个动作(在返回的内容之下):
Response.ContentType = "application/json;charset=utf-8";
Response.StatusCode = (int)(packetsLost < 4 ? HttpStatusCode.OK : HttpStatusCode.NotFound);
Response.TrySkipIisCustomErrors = true;
return new JsonResult()
{
ContentType = "application/json;charset=utf-8",
Data = new
{
sent = 4,
received = 4 - packetsLost,
lost = packetsLost,
percentLost = (int) (packetsLost / 4 * 100),
responseStatus01 = statuses[0],
responseStatus02 = statuses[1],
responseStatus03 = statuses[2],
responseStatus04 = statuses[3],
responseMessage01 = responseStatuses[0],
responseMessage02 = responseStatuses[1],
responseMessage03 = responseStatuses[2],
responseMessage04 = responseStatuses[3],
rtt01 = rtt[0],
rtt02 = rtt[1],
rtt03 = rtt[2],
rtt04 = rtt[3],
ttl01 = ttl[0],
ttl02 = ttl[1],
ttl03 = ttl[2],
ttl04 = ttl[3],
minRtt = roundTripTimes.Count == 0 ? "0" : string.Format("{0}ms", roundTripTimes.Min()),
maxRtt = roundTripTimes.Count == 0 ? "0" : string.Format("{0}ms", roundTripTimes.Max()),
avgRtt = roundTripTimes.Count == 0 ? "0" : string.Format("{0}ms", roundTripTimes.Average())
},
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
和jquery ajax调用:
ajax({
url: '/Controller/Action/',
type: 'POST'
}).then(function (data) {
var _data;
try {
_data = $.parseJSON(data);
}
catch (err) {
alert("Something went bad!!!");
}
}, function (data) {
var _data;
try {
_var = $.parseJSON(data.responseText);
}
catch (err) {
alert("Something went bad!!!");
}
});
1)当操作返回http状态代码OK(200)时,jquery中的data.responseText ajax回调为空,所以我改为转换数据(包含正确的数据)但是由于某种原因它在使用parseJSON(数据)进行转换时崩溃了:错误捕获说无效字符......我不知道为什么......
2)当动作返回http状态代码NotFound(404)时,data.responseText有时(并非总是)为空,而数据中的statusText为“错误”,所以我都不明白....
控制器中的操作始终返回正确的数据....
有人可以帮助我检测此错误吗?或者让我朝着正确的方向前进......
更新: 从ie dev工具,在网络中,我观察到一些ping被取消,然后当试图处理它时,它返回空的responseText到ajax回调,所以当试图解析它时,它崩溃了。我观察到如果我将ping的超时设置为较低的值,例如,750ms,它正在工作,但如果我使用ping的默认超时,即5秒,则它不起作用:某些ping被取消并且当试图为它们提供服务时,它们返回空的responseText,导致它在尝试解析ajax回调时崩溃。