Reputation: 1775
Is there a way to determine if the controller is sending back a simple Json string, or an encoded object?
If the information is returned correctly it will be sent as an encoded object...
Return Json(vResult, JsonRequestBehavior.AllowGet)
But is there is an exception a simple string...
Return Json(UnhandledError)
How can I check to see which is being returned?
Something like this
success: function (response) {
if (response is Json object) { // This is the problem bit
$('#textareaDescription').val(response.Description);
}
else {
ModalError(response);
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
ModalError(textStatus + " - " + errorThrown);
}
Thank you
Upvotes: 0
Views: 41
Reputation: 24965
After talking with you in the comments, it appears that the ajax automatically parsing the response in the good case. I'm assuming that in the bad case it is not attempting to parse it, otherwise it would encounter an error before it got into your success handler.
This could be the case if the service endpoint, for the good case, is setting the Content-Type on the response to json so that jQuery knows to auto parse it for you. In any case, you can use that to your advantage.
if (typeof response === 'string') {
//do logic for bad response
} else {
//do logic for good response
}
Or you could potentially flip it and check for the typeof to be 'object'.
Upvotes: 1