My application making an ajax POST to server, and if the server validation fails, server returns string
or Dictionary<string, object>
back to client.
So if server is sending Dictionary
then the serialized responseText
that jQuery is receiving something like
"{\"Key1\":[\"Error Message 1\"],\"Key2\":[\"Error message 2\"]}"
i also have corresponding responseJSON
available on client side.
$.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
}
}
}
The .isArray
method returns false when response is dictionary. How do i determine if responseJSON is Dictionary
and how do i loop?
Note
object that server is sending back