如何使用 $.ajax 和 return $.each 循环中的值读取 XML 数据
How to read XML data with $.ajax and return the value within a $.each loop
对于我们研究项目的一部分,我们需要将字符串数组与 xml 数据库进行比较。所以我的想法是分成两部分(因为我们需要两次比较函数)。首先,我用 $.each 遍历数组,然后将值传递给另一个函数,该函数向 xml 发出 ajax 请求,将值与 xml 中的每个数据进行比较,如果找到的东西,它将它推入一个数组,该数组应该在最后返回。
在一个函数中调用 jquery.each():
function doSomething(){
liste = ["test1","test32","test22"];
$.each(liste, function(index, value){
var checkIt = compareDataBase(value);
if(checkIt.length>0) //do Something
});
}
这是比较值和 returns 数组的函数:
function compareDataBase(foo){
var lists = [];
$.ajax({
type:"GET",
url: "data/database/datenbank.xml",
dataType:"xml",
success: function(xml){
$(xml).find('product').each(function(index, element){
var current_product = $(this).text();
if(current_product.indexOf(foo)>-1)lists.push(current_product);
});
},
error: function(){
console.log("Fehler bei Produkte auslesen");
}
});
return lists;
}
但遗憾的是,这不起作用。 "checkIt" 始终未定义,因为它不等待 ajax...
我尝试使用 $.when 函数或给 compareDataBase() 函数一个回调,但不知何故两者都不起作用(我想是因为
我声明错了)
也许有人知道如何做到这一点?
感谢您的帮助!
br sebi
您应该使用回调(或 promises 回调的变体),以下解决方案示例使用回调:
function compareDataBase(foo, callback){
var lists = [];
$.ajax({
type:"GET",
url: "data/database/datenbank.xml",
dataType:"xml",
success: function(xml){
$(xml).find('product').each(function(index, element){
var current_product = $(this).text();
if(current_product.indexOf(foo)>-1)lists.push(current_product);
});
// notify the callback with the result lists here
callback(lists);
},
error: function(){
console.log("Fehler bei Produkte auslesen");
}
});
}
function doSomething(liste, index){
if ( index < liste.length )
{
compareDataBase(liste[index], function(checkIt){
if(checkIt.length>0) //do Something
// process next list item using callbacks
doSomething(liste, index+1);
});
}
}
// start the process
doSomething(["test1","test32","test22"], 0);
注意示例解决方案仅在前一个项目已处理后才处理每个列表项(它有点同步回调,每个回调将调用下一个)。可以删除此功能并按如下方式异步处理所有内容:
// process all async
var liste = ["test1","test32","test22"];
for (var i=0; i<liste.length; i++) doSomething([liste[i]], 0);
对于我们研究项目的一部分,我们需要将字符串数组与 xml 数据库进行比较。所以我的想法是分成两部分(因为我们需要两次比较函数)。首先,我用 $.each 遍历数组,然后将值传递给另一个函数,该函数向 xml 发出 ajax 请求,将值与 xml 中的每个数据进行比较,如果找到的东西,它将它推入一个数组,该数组应该在最后返回。
在一个函数中调用 jquery.each():
function doSomething(){
liste = ["test1","test32","test22"];
$.each(liste, function(index, value){
var checkIt = compareDataBase(value);
if(checkIt.length>0) //do Something
});
}
这是比较值和 returns 数组的函数:
function compareDataBase(foo){
var lists = [];
$.ajax({
type:"GET",
url: "data/database/datenbank.xml",
dataType:"xml",
success: function(xml){
$(xml).find('product').each(function(index, element){
var current_product = $(this).text();
if(current_product.indexOf(foo)>-1)lists.push(current_product);
});
},
error: function(){
console.log("Fehler bei Produkte auslesen");
}
});
return lists;
}
但遗憾的是,这不起作用。 "checkIt" 始终未定义,因为它不等待 ajax... 我尝试使用 $.when 函数或给 compareDataBase() 函数一个回调,但不知何故两者都不起作用(我想是因为 我声明错了)
也许有人知道如何做到这一点?
感谢您的帮助!
br sebi
您应该使用回调(或 promises 回调的变体),以下解决方案示例使用回调:
function compareDataBase(foo, callback){
var lists = [];
$.ajax({
type:"GET",
url: "data/database/datenbank.xml",
dataType:"xml",
success: function(xml){
$(xml).find('product').each(function(index, element){
var current_product = $(this).text();
if(current_product.indexOf(foo)>-1)lists.push(current_product);
});
// notify the callback with the result lists here
callback(lists);
},
error: function(){
console.log("Fehler bei Produkte auslesen");
}
});
}
function doSomething(liste, index){
if ( index < liste.length )
{
compareDataBase(liste[index], function(checkIt){
if(checkIt.length>0) //do Something
// process next list item using callbacks
doSomething(liste, index+1);
});
}
}
// start the process
doSomething(["test1","test32","test22"], 0);
注意示例解决方案仅在前一个项目已处理后才处理每个列表项(它有点同步回调,每个回调将调用下一个)。可以删除此功能并按如下方式异步处理所有内容:
// process all async
var liste = ["test1","test32","test22"];
for (var i=0; i<liste.length; i++) doSomething([liste[i]], 0);