Loading XML into a browser with jQuery
Does anyone know why the following code does not work in FireFox but does in IE (on the server) and the other way around locally?
function load_xml(msg) { //this function will load xml even used in IE or any other browser
if ( typeof msg == 'string') {
data = new ActiveXObject( 'Microsoft.XMLDOM');
data.async = false;
data.loadXML( msg);
} else {
data = msg;
}
return data;
}
function getTitle(letter) {
$('#wordle').html('');
jQuery.ajax({
type: "POST",
url: "wordle-list.dat",
d开发者_Go百科ataType: ($.browser.msie) ? "text/xml" : "xml",
success: function(xml) {
var xml2 = load_xml(xml);
var i=0;
$(xml2).find('wordle').each(function(){
$(xml2).find('w').each(function(){ //can change to w:lt(50)
var tmpHold = $(this).text();
if (tmpHold.substring(0, 1) == letter) {
$('#wordle').append('<li class="w">'+$(this).text()+'</li>');
}
});
});
}
});
}
My guess is because you have ActiveX installed in IE on the server and not on firefox and vice versa on your machine. Though it's difficult to say from just the code. At what line does the code fail both on the server and on the client machine?
Try this:
function load_xml(msg) {
if ( typeof msg == 'string') {
if (window.DOMParser)//Firefox
{
parser=new DOMParser();
data=parser.parseFromString(msg,"text/xml");
}else{ // Internet Explorer
data=new ActiveXObject("Microsoft.XMLDOM");
data.async="false";
data.loadXML(msg);
}
} else {
data = msg;
}
return data;
}
FOR EVERYONE WONDERING:
It was because I had the file extension as .dat
and the server was saying it was a binary mime type...
精彩评论