[英]JQuery AJAX return XML on GET request complete
成功返回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文件?
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
});
});
這是原始詢問者添加的編輯內容,我將其轉換為社區Wiki答案,因為它應該是答案,而不是編輯內容。
由於@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
部分中這樣做會更有意義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.