来自多个ajax请求的HTML数据到javascript数组

发布于 2024-12-02 23:39:34 字数 1064 浏览 0 评论 0原文

我正在尝试使用 AJAX 和 jQuery 预加载一些 html 内容。 AJAX 回调函数将数据添加到关联数组中。如果我单独执行每个请求,我很好:

var contentArray = new Object();

var urlA = "includes/contentA.php";
var urlB = "includes/contentB.php";
var urlC = "includes/contentC.php";

$.get(urlA, function(htmlA) {
    contentArray["A"] = htmlA;
});
$.get(urlB, function(htmlB) {
    contentArray["B"] = htmlB;
});
$.get(urlC, function(htmlC) {
    contentArray["C"] = htmlC;
});

因为我可能有其中一些(超过三个),所以我尝试用 for 循环来执行它:

var contentArray = new Object();
var pages = new Object();

pages["A"] = "includes/contentA.php";
pages["B"] = "includes/contentB.php";
pages["C"] = "includes/contentC.php";

for (var key in pages) {
    var URL = pages[key];
    $.get(URL, function(html) {
        contentArray[key] = html;
    });
}

但是,这不起作用。 contentArray 只有一个包含 html 数据的属性,而不是三个。我了解 jQuery,特别是 AJAX 的东西,所以解释和解决方案(类似或不同方法相同结果)都很受欢迎。

顺便说一句,我知道一个较大的 AJAX 请求比多个较小的 AJAX 请求更好,但我试图为未启用 JS 的用户保留兼容性,并且当前的 php include 很方便。关于如何满足这两个要求的任何建议也非常受欢迎。

谢谢。

I'm trying to pre-load some html content using AJAX and jQuery. The AJAX callback function adds the data to an associative array. I'm fine if I do each request individually:

var contentArray = new Object();

var urlA = "includes/contentA.php";
var urlB = "includes/contentB.php";
var urlC = "includes/contentC.php";

$.get(urlA, function(htmlA) {
    contentArray["A"] = htmlA;
});
$.get(urlB, function(htmlB) {
    contentArray["B"] = htmlB;
});
$.get(urlC, function(htmlC) {
    contentArray["C"] = htmlC;
});

Since I am likely to have a few of these (more than three), I tried to do it a for loop:

var contentArray = new Object();
var pages = new Object();

pages["A"] = "includes/contentA.php";
pages["B"] = "includes/contentB.php";
pages["C"] = "includes/contentC.php";

for (var key in pages) {
    var URL = pages[key];
    $.get(URL, function(html) {
        contentArray[key] = html;
    });
}

However, this doesn't work. contentArray only has one property containing html data, rather than three. I'm knew to jQuery, particularly the AJAX stuff, so both explanations and solutions (similar or different-method-same-result) are welome.

By the way, I'm aware that one larger AJAX request is preferable to multiple small ones, but I'm trying to retain compatibility for users without JS enabled, and the current php includes are convenient. Any suggestions as how I might satisfy both these requirements are also very welcome.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇纵 2024-12-09 23:39:34

AJAX 请求的回调函数在请求返回之前不会运行。在您的情况下,每个回调函数将使用当前上下文中存在的 key ,并且由于其本地范围内没有 key 变量,它将使用它能找到的最接近的变量, for 循环中的 key

问题是,当 AJAX 请求返回时,for 循环已完全迭代,并且 key 等于数组中的最后一个键。因此,每个回调函数都将接收相同的key,并覆盖contentArray中先前的值。

如果您使用的是 jQuery 1.5.1 或更高版本,一个快速而肮脏的解决方案(不涉及更改 PHP 文件的当前结构)可能是尝试以下操作:

for (var key in pages) {
  var URL = pages[key];

  $.ajax({
    url: URL,
    xhrFields: {
      'customData': key
    },
    success: function(html, statusText, jqXHR) {
      contentArray[jqXHR.customData] = html;
    }
  });
}

我还没有测试过,但根据 < a href="http://api.jquery.com/jQuery.ajax/" rel="nofollow">文档页面 它应该可以工作。您所做的就是使用 jQuery 创建的请求对象将变量传递给回调函数。

希望有帮助

The callback function for an AJAX request doesn't run until the request returns. In your case each callback function will use key as it exists in the current context, and since there's no key variable in it's local scope it will use the nearest it can find, the key in your for loop.

The problem is by the time the AJAX requests return, the for loop has been fully iterated over and key is equal to the last key in the array. Thus each of the callback functions will receive the same key, overwriting the previous value in your contentArray.

If you're using jQuery 1.5.1 or above a quick and dirty solution (one that doesn't involve changing the current structure of your PHP files) might be to try the following:

for (var key in pages) {
  var URL = pages[key];

  $.ajax({
    url: URL,
    xhrFields: {
      'customData': key
    },
    success: function(html, statusText, jqXHR) {
      contentArray[jqXHR.customData] = html;
    }
  });
}

I haven't tested that but according to the documentation page it should work. All you're doing is using the request object created by jQuery to pass your variable along to the callback function.

Hope that helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文