JQuery AJAX在GET请求上返回XML完成

一旦我的XML文件(或对象)成功返回,我需要执行代码。 不是之前,而是不是重复; 文件返回只有一次。

我的代码是否已经完成了我想要完成的任务? 它似乎在IE中失败了几次,我需要确保它是可靠的。

$(document).ready(function(){ (function GetFile(){ $.ajax({ type: "GET", url: "Produce.xml", dataType: "xml", cache: false, success: function(result) { alert("XML File is loaded!"); alert(result); }, async: true }); })(); }); 

我知道有可以检查和validation的错误代码和readystatechange值…在轮询服务器以查找XML文件时,我应该检查这些值吗?

删除async: true后的逗号async: true

此外,如果您不打算再次调用,您的GetFile函数将立即执行,然后可能同时使用匿名或删除该函数

 $(document).ready(function(){ $.ajax({ type: "GET", url: "Produce.xml", dataType: "xml", cache: false, success: function(result) { alert("XML File is loaded!"); alert(result); }, async: true }); }); 

这是作为原始提问者的编辑添加的,我已将其转换为社区维基答案,因为它应该是答案,而不是编辑。

修复它感谢@AndrewDouglas建议,这里是新代码(效果很好):

 $(document).ready(function(){ (function GetFile(){ $.ajax({ type: "GET", url: "Produce.xml", dataType: "xml", cache: false, success: function(result) { alert("XML File is loaded!"); alert(result); }, error:function (xhr, ajaxOptions, thrownError){ z++; alert(xhr.status); alert(thrownError); setTimeout(GetFile, 5000); console.log("Error " +z+": " + thrownError); }, async: true }); })(); }); 

最后一条评论,您应该能够将setTimeout(GetFile, 5000)更改为setInterval(GetFile, 5000) ,然后它将继续轮询您的XML文件。 然而,在success部分中这样做会更有意义。