Can you handle both json and html datatypes in the same ajax call?

Posted by Prabhu on Stack Overflow See other posts from Stack Overflow or by Prabhu
Published on 2011-03-02T23:21:38Z Indexed on 2011/03/02 23:25 UTC
Read the original article Hit count: 426

Filed under:
|
|
|
|

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?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about .NET