Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.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_Callback_Nested - Fatal编程技术网

使用Javascript通过嵌套函数返回数组

使用Javascript通过嵌套函数返回数组,javascript,callback,nested,Javascript,Callback,Nested,各位晚上好。我知道这对这里的许多人来说似乎是一个非常容易的主题,但我已经努力了一段时间来重建我的功能,以便使其在整个站点范围内动态且可重用 我面临的主要困难是使用以下命令返回数组: var arr = getData("admin/fullDatabaseAccess.php"); 这不会按预期返回数组。现在,在尝试返回它创建的数组时,我没有编写我对getData函数所做的每一个可能的变化,我将首先向您展示运行的原始函数: function getData() { var xmlhttp =

各位晚上好。我知道这对这里的许多人来说似乎是一个非常容易的主题,但我已经努力了一段时间来重建我的功能,以便使其在整个站点范围内动态且可重用

我面临的主要困难是使用以下命令返回数组:

var arr = getData("admin/fullDatabaseAccess.php");
这不会按预期返回数组。现在,在尝试返回它创建的数组时,我没有编写我对getData函数所做的每一个可能的变化,我将首先向您展示运行的原始函数:

function getData() {

var xmlhttp = new XMLHttpRequest();
var url = "admin/fullDatabaseAccess.php";

xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        processResponse(xmlhttp.responseText);
    }
}
xmlhttp.open("GET", url, true);
xmlhttp.send();

function processResponse(response) {


    var arr = JSON.parse(response);

    // do stuff with the array - I originally modified the DOM using jQuery here, however I now want to use this entire getData function as a more generically useful function

    }

}
然后,我将在使用此代码生成数组的单个页面上触发getData函数,然后使用数组数据修改页面元素

这就引出了我的问题-我试图通过创建以下版本,并使用我最初发布的代码行(var arr=…)调用数组数据,使此函数在整个站点上可重用:

}

我似乎无法将数据反馈给变量。我曾多次尝试重新构造函数,以返回嵌套中的值等,但我已经到了让自己感到困惑的地步,无法真正显示我尝试过的示例,因为我删除了它们并决定重新开始


任何帮助都将不胜感激

您需要提供对
getData
的回调,如下所示

function getData(file, cb) {
  var xmlhttp = new XMLHttpRequest();
  var url = file;

  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      // Calls the callback function with the response data
      cb(processResponse(xmlhttp.responseText));
    }
  }

  xmlhttp.open("GET", url, true);
  xmlhttp.send();

  function processResponse(response) {
    var arr = JSON.parse(response);
    return arr;

  }
}

getData(file, function(data) {
  // data is now what's being returned from processResponse()
});

我很感激!专门针对processResponse()的回调是我从未创建过的缺少的链接!!谢谢你的帮助,一切都很好。
function getData(file, cb) {
  var xmlhttp = new XMLHttpRequest();
  var url = file;

  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      // Calls the callback function with the response data
      cb(processResponse(xmlhttp.responseText));
    }
  }

  xmlhttp.open("GET", url, true);
  xmlhttp.send();

  function processResponse(response) {
    var arr = JSON.parse(response);
    return arr;

  }
}

getData(file, function(data) {
  // data is now what's being returned from processResponse()
});