最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Different behavior for empty XML response between IE and Firefox - Stack Overflow

programmeradmin6浏览0评论

I'm using jQuery to read an XML file. Sometimes the XML is empty, and I expect the error function (no_info) is executed because the file is not formatted according to the dataType.

In IE 10 the Error function is executed. But in Firefox (40.0.2) the success function (parse) is executed. Why both browsers behave differently and which one is correct?

$.ajax({
  url: '/~play/shout.xml',
  dataType: "xml",
  success: parse,
  error: no_info
});

I'm using jQuery to read an XML file. Sometimes the XML is empty, and I expect the error function (no_info) is executed because the file is not formatted according to the dataType.

In IE 10 the Error function is executed. But in Firefox (40.0.2) the success function (parse) is executed. Why both browsers behave differently and which one is correct?

$.ajax({
  url: '/~play/shout.xml',
  dataType: "xml",
  success: parse,
  error: no_info
});
Share Improve this question edited Dec 12, 2018 at 8:40 Ivar 6,88712 gold badges56 silver badges67 bronze badges asked Sep 9, 2015 at 20:58 Nico van WijkNico van Wijk 2691 silver badge9 bronze badges 10
  • 5 IE being IE, did you try it without the '~'? I just gave up on it.. when my team develops websites/services, we always put that jumbotron up there with "get yourself some chrome/firefox" message. – Toza Commented Sep 9, 2015 at 21:03
  • I think this is happening because there might be an unescaped character or new line character in the file, where browsers interpret them differently. Can you check your xml to see if there is a newline? If so remove it and try again. – afrin216 Commented Sep 9, 2015 at 21:12
  • No NemanjaT thats not the problem. The xml file read correct. I can read the nodes from the file. But when the XML file is empty, Firefox goes tot the parse function and IE goes to the no_info function. And thats strange.... – Nico van Wijk Commented Sep 9, 2015 at 21:27
  • 5 Wele to the world of cross-browser-patibility-pain :) – afrin216 Commented Sep 9, 2015 at 22:01
  • 1 Can you please post the error – brk Commented Oct 15, 2015 at 12:37
 |  Show 5 more ments

4 Answers 4

Reset to default 2

Looks like there's a bug in IE

how about you handle it yourself?

 function parseXml(xml) {

   if ($.browser.msie)  {

   if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
     xmlhttp = new XMLHttpRequest();
   }else {// code for IE6, IE5
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   }
   xmlhttp.open("GET", "XML_file.xml", false);
   xmlhttp.send();
xmlDoc = xmlhttp.responseXML;

xml = xmlDoc;
} 
return xml;

}

previous answer

which JQuery version do you use? I use the most actual and with my ajax function I couldn't encounter any issues. That's my code

function sync(arg, callback){   //ajax result 
    $('.loader').show();
    $.ajax({ 
        method: 'GET',
        url: 'liveSearch.php',
        data: arg, // send argument and update
        success: function(data, status, xhr){
         $('.loader').hide();
         callback(data);
        },
        error: function(xhr, ajaxOptions, thrownError){
            console.log(thrownError);
        }
   });  
}

function onCallback(data) {
        result = data;
}

dataType parameter merely indicates what "Content-Type" header you are expecting. As long as the file exists and served with a valid Content-Type Success function should be triggered.

instead of just /~ try passing the whole URL from which you want to retrieve the XML file.

发布评论

评论列表(0)

  1. 暂无评论