阿贾克斯/ jQuery的到PHP,然后再返回?然后再、阿贾克斯、jQuery、PHP

2023-09-10 18:19:53 作者:浮生寄旧梦

我有一个Ajax调用使用jQuery做,是这样的:

I have an ajax call made with jQuery, something like this:

$.ajax({
    type: "POST",
    url: ajaxurl,
    data: data,
    success: function(response){
        alert(response);
    }
});

我得到的数据从PHP这样的:

I get data from PHP like this:

$data_array = get_data();
foreach($data_array as $data)
{
    echo $data;
}

PHP慢

PHP数据的函数调用速度较慢,因为它从数据库中获取非常多的数据,可能会获取图像,使一些JSON通话等慢的东西。

The PHP data function call is slow because it gets very much data from the database, might fetch images, make some json calls and other slow things.

一环轮的时候

为此我需要的PHP code只是做一个回合,然后使用Javascript,然后做循环下一轮。

Therefor I need to get the PHP code just do one round, then Javascript and then do the next round in the loop.

解决这个问题不止一个方法是什么?

可能会有这样做的方法不止一种。哪一个是prefered?使用Javascript的foreach循环,JSON和全局JavaScript变量浮现在脑海。

There might be more than one way to do it. Which one is prefered? Javascript foreach-loop, JSON and global Javascript variables comes to mind.

推荐答案

您可以设置您的AJAX功能的XML或JSON的返回类型,然后返回你数组中任一这些类型。我觉得JSON JS的preferred适合您的解决方案。

You can set the return type in your ajax function as xml or json, and then return you array in either of these types. I feel that JSON js the preferred one for your solution.