Return 通过 Javascript 的嵌套函数返回数组
Return array back through nested functions with Javascript
大家晚上好。我知道这对这里的许多人来说似乎是一个非常简单的主题,但我已经努力重建我拥有的功能以使其在整个站点范围内动态和可重用。
我遇到的主要问题是return使用以下方法创建数组:
var arr = getData("admin/fullDatabaseAccess.php");
这不是 return 预期的数组。现在,我不写我对 getData 函数所做的所有可能的变体,试图 return 它创建的数组,我将首先向您展示原始函数作品:
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 = ..):
function getData(file) {
var xmlhttp = new XMLHttpRequest();
var url = file;
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);
return arr;
}
}
我似乎无法将数据反馈给变量。我已经尝试将函数重组为 return 嵌套等中的值,但我已经到了让自己感到困惑并且不能真正做到的地步显示我删除它们并决定重新开始时尝试的示例。
如有任何帮助,我们将不胜感激!!
您需要向 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()
});
大家晚上好。我知道这对这里的许多人来说似乎是一个非常简单的主题,但我已经努力重建我拥有的功能以使其在整个站点范围内动态和可重用。
我遇到的主要问题是return使用以下方法创建数组:
var arr = getData("admin/fullDatabaseAccess.php");
这不是 return 预期的数组。现在,我不写我对 getData 函数所做的所有可能的变体,试图 return 它创建的数组,我将首先向您展示原始函数作品:
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 = ..):
function getData(file) {
var xmlhttp = new XMLHttpRequest();
var url = file;
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);
return arr;
}
}
我似乎无法将数据反馈给变量。我已经尝试将函数重组为 return 嵌套等中的值,但我已经到了让自己感到困惑并且不能真正做到的地步显示我删除它们并决定重新开始时尝试的示例。
如有任何帮助,我们将不胜感激!!
您需要向 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()
});