我的应用程序向服务器发送ajax POST,如果服务器验证失败,服务器将string
或Dictionary<string, object>
返回给客户端。如何确定json对象是否是序列化字典?
因此,如果服务器发送Dictionary
然后系列化responseText
是jQuery是收到类似
"{\"Key1\":[\"Error Message 1\"],\"Key2\":[\"Error message 2\"]}"
我也有相应的可在客户端responseJSON
。
$.ajax({
cache: false,
type: 'POST',
url: url,
data: data
})
.fail(function (response, textStatus, errorThrown) {
if (response.status === '400') {
if ($.isArray(response.responseJSON)) {
$.each(response.responseJSON, function (index, value) {
//do something
})
}
else if ($.type(response.responseJSON) === 'string') {
// do something
}
}
}
当响应是字典时,.isArray
方法返回false。我如何确定responseJSON是否为Dictionary
以及我如何循环?
注意
object该服务器发回
的可能的复制[检查如果一个值是在JavaScript对象(http://stackoverflow.com/questions/8511281/check-if-a-value-is-an-object-in-javascript ) – Hamms
JavaScript中没有'Dictionary'类型。你得到一个JSON字符串的方式。一旦反序列化,你就有一个“对象”。 –
你在做什么没有意义。将dataType设置为json并使用成功处理程序处理已经是对象的数据。如果失败,则responseText无效json或者有其他连接错误 – charlietfl