阿贾克斯/ jQuery的到PHP,然后再返回? [英] Ajax / jQuery to PHP and back again?

查看:148
本文介绍了阿贾克斯/ jQuery的到PHP,然后再返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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.

这篇关于阿贾克斯/ jQuery的到PHP,然后再返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆