Can you handle both json and html datatypes in the same ajax call?
- by Prabhu
Is there anyway I can handle both json and html return types when posting jquery ajax:
For example, this ajax call expects html back
$.ajax({
type: "POST",
url: url
data: data,
dataType: "html",
success: function (response) {
var $html = "<li class='list-item'>" + response + "</li>";
$('#a').prepend($html);
},
error: function (xhr, status, error) {
alert(xhr.statusText);
}
});
but I wanted to modify it so that I can return a json object if there is a model error. so I can do something like this:
success: function (response) {
if (response.Error){
alert(response.Message);
} else {
var $html = "<li class='list-item'>" + response + "</li>";
$('#a').prepend($html);
}
Is this possible?