我如何知道/测试来自 jquery ajax 的返回数据是 xml、html 还是纯文本?
例如,我有这两种类型的数据要处理。
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;
});
所以,
if(returndata.isXML) alert("xml");
if(returndata.isHTML) alert("html");
是否可以?