Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从javascript返回数组_Javascript_Jquery_Arrays_Ajax - Fatal编程技术网

从javascript返回数组

从javascript返回数组,javascript,jquery,arrays,ajax,Javascript,Jquery,Arrays,Ajax,因此,在6个多小时的搜索和尝试各种解决方案之后,我只能在console.log(diseaseNameArray)中记录“undefined”。我对javascript非常陌生,无法理解为什么不记录填充的数组。我非常感谢您提供的任何帮助/建议。对代码的任何其他附加评论也将不胜感激 var diseaseNameArray; var jax = $.ajax; /*OutputHandler*/ function outputHandler(data, arrayTarget, tag, len

因此,在6个多小时的搜索和尝试各种解决方案之后,我只能在console.log(diseaseNameArray)中记录“undefined”。我对javascript非常陌生,无法理解为什么不记录填充的数组。我非常感谢您提供的任何帮助/建议。对代码的任何其他附加评论也将不胜感激

var diseaseNameArray;
var jax = $.ajax;

/*OutputHandler*/
function outputHandler(data, arrayTarget, tag, length) {
    var source = $(data); /*holder of xml*/
    var lengthStorage = Number(source.find(length).text()); /*length of list*/
    arrayTarget = []; //array to be populated
    for(i = 1; i < lengthStorage; i++)
    {
        arrayTarget[i] = source.find(tag + i.toString()).text();
        console.log(arrayTarget[i]); //to check that elements are being entered
    }
    console.log(arrayTarget); //succesfully logs the full array
}

/*General function*/
function populateArray(xmlLocation, typeOfData, tag, lengthStorage, extFunction, targetArray) {     
    $.ajax({
        type: "GET",
        url: xmlLocation,
        dataType: typeOfData,
        success: function(xml)
        {
            extFunction(xml, targetArray, tag, lengthStorage);
        },
        error: function()
        {
            console.log("ugh");
        }
    });
}

populateArray("malePatient.xml", "xml", "sub", "length", outputHandler, diseaseNameArray);
console.log(diseaseNameArray);
var-diseaseNameArray;
var jax=$.ajax;
/*外卖手*/
函数输出处理器(数据、阵列目标、标记、长度){
var source=$(数据);/*xml持有者*/
var lengthStorage=Number(source.find(length.text());/*列表长度*/
arrayTarget=[];//要填充的数组
对于(i=1;i
更新
多亏了简的观点,我又回到了绘图板上,让它工作起来。如果有人有什么建议,我将不胜感激

Ajax是异步的。您正在调用
populateArray
,然后在ajax请求完成之前立即将
diseaseNameArray
记录到控制台,因此它当然是未定义的——您没有给ajax请求时间来完成。您需要在
success
中登录到console


或者使ajax调用与
async:false
同步。ajax代表AsynchronousJavascriptAndXml。因为它是异步的,所以数据不一定到达
outputHandler
函数。您可能应该创建一个在
outputHandler
之后运行的函数,并在
outputHandler
末尾调用。您应该将
console.log
放在第二个函数中。这样,就可以保证获得数据


或者,您可以将
async:false
传递给AJAX调用以使其同步,这样console.log将在
outputHandler
之后出现。但是,如果AJAX调用失败,那么console.log仍将被调用,并且它将是未定义的。

使用这个问题,我能够创建一个游戏函数,该函数将能够访问大量xml文档并利用收集的数据。

Paul感谢您的回答!我显然需要深入研究文档,因为从阅读教程中,我认为只有在检索到所有数据后才能成功运行。所以我在一个浏览器内游戏中使用这个数组。异步调用的缺点仅仅是页面加载时间吗?正确的是,success()只会在检索数据后运行。但是您的console.log(diseaseArrayName)不在success函数中,因此事件顺序为“获取数据”,然后是“console.log尚未到达的数据”,然后是“数据已经到达,调用success()!”谢谢您的回答。我在那篇评论中有一个拼写错误。我想问的是,同步通话是否有负面影响?关于使用同步优于异步,是否有任何不变的规则?还是更主观。我找到了这个。这是有帮助的,但自从那份摘要更新以来已经有一段时间了。只是想看看是否有任何普遍接受的do和do NOT?Jquery的
async=false
选项即将被弃用,所以我建议不要使用它。现实世界中也很少有绝对必要的应用程序。@Jan谢谢你提供的信息,我不知道async=false离斩波块有多近。在这种情况下,您能否举例说明如何正确实施这一点?或者甚至是一个拉取数据并在成功函数之外的其他地方使用它的示例?通过ajax查看jQuery文档后,没有灯泡不幸熄灭。请查看文档的
部分