Pregunta

How can I know/ test the return data from jquery ajax is a xml, html or a plain text?

For instance, I have these two types of data that I want to handle.

xml,

<?xml version="1.0"?>
<xml><response><error elementid="accept_terms_conditions" message="Field 'Agree with Terms &amp; Conditions' needs to be filled."/></response></xml>

html,

<form action="http://xxx/booking.php" method="post" enctype="multipart/form-data" class="item-form border-top-bottom">
...
</form>

jquery,

$(".button-submit").click(function(){

    var form = $(this).closest("form");
    var url = form.attr("action");

    // Load the article into the object.
    $.ajax({
        type: "POST",
        //dataType: "html",
        url: url,
        data:form.serialize(),
        context:$(".holder-form-book-online"),
        async: false,
        beforeSend: function() {
            //
        },
        success: function (returndata) {

                 if(returndata.isXML) alert("xml");
                 if(returndata.isHTML) alert("html");

        }

    }).fail(function() { 
            alert("error"); 
    });

    return false;

});

So,

if(returndata.isXML) alert("xml");
if(returndata.isHTML) alert("html");

Is it possible?

¿Fue útil?

Solución 2

 var contType = returndata.getResponseHeader ("Content-Type");
 if(contType == "") { // take it from here... }

Otros consejos

In the complete function in your $.ajax settings you can get the response headers like so,

complete: function(data){ 
    console.log(data.getResponseHeader('Content-type')); 
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top