How to handle JSON and HTML together?

Via AJAX i am requesting certain informaiton. The script either returns the content as html or in case of an error returns JSON (error number and description).

How can i fix the success message to cater for both?


$.ajax({
            cache: false,
            type: "POST", //POST
            url: processPageURL,
            data: dataValues, //form seralize
            dataType: "html",
            error: function (xhr, ajaxOptions, thrownError) {
    alert(xhr.responseText);
            },
            success: function (data) {
                alert(data);
            }
        });

Thanks

Thanks.

This may be a little late…

When you remove the “dataType” jQuery will try to figure out what type of data is being returned by reading the header information.

So what you can do is the following


$.ajax({
    cache: false,
    type: "POST", //POST
    url: processPageURL,
    data: dataValues, //form seralize
    success: function (data) {
        if (typeof data == 'object') { JSON
            alert('We got some JSON!');
        }
        else { //html
            alert('We got some HTML!');
        }
    }
});