我的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
});
})();
});
我知道有onerror
个代码和readystatechange
值可以检查和验证...我应该在为服务器查询XML文件时检查这些值吗?
答案 0 :(得分:1)
在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
});
});
答案 1 :(得分:0)
这是作为原始提问者的编辑添加的,我已将其转换为社区维基答案,因为它应该是答案,而不是编辑。
通过@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
部分中执行该操作会更有意义。