Reputation: 57176
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 & 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?
Upvotes: 1
Views: 1164
Reputation: 3289
var contType = returndata.getResponseHeader ("Content-Type");
if(contType == "") { // take it from here... }
Upvotes: 1
Reputation: 5788
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'));
}
Upvotes: 2