我跟着从这个网页教程:jquery ajax请求多个返回值?
http://www.keyboardninja.eu/webdevelopment/jquery-ajax-call-tutorial
,并已经得到了随机数输出。但是,我怎样才能改变函数(和PHP文件?),我得到多个返回(例如“msg”)来填充不同的div?或者这是不可能的,所以我需要填充包含div的东西,并将大量代码放入php文件的echo部分中?
感谢
我跟着从这个网页教程:jquery ajax请求多个返回值?
http://www.keyboardninja.eu/webdevelopment/jquery-ajax-call-tutorial
,并已经得到了随机数输出。但是,我怎样才能改变函数(和PHP文件?),我得到多个返回(例如“msg”)来填充不同的div?或者这是不可能的,所以我需要填充包含div的东西,并将大量代码放入php文件的echo部分中?
感谢
,添加的所有消息在阵列比转换为JSON,如下面给出的。在Ajax调用
$msg=array();
$msg[] = $msg1;
$msg[] = $msg2;
.
.
........
echo json_encode($msg);
现在处理JSON在JavaScript中使用每个显示所有邮件的
试试这个,
在你的Ajax文件,追加与多个消息 '|'或'〜',当它返回时,将其拆分为JS;并通过innerHTML将其放置在您的DIV中。
EX(在AJAX)
echo $msg1.'|'.$msg2.'|'.$msg3;
简单做这在PHP端(ajax.php):
$arr = array();
$arr['msg1'] = '<p>Hi I am some random ' . rand() .' output from the server First Msg.</p>';
$arr['msg2'] = '<p>Hi I am some random ' . rand() .' output from the server Second Msg.</p>';
$arr['msg3'] = '<p>Hi I am some random ' . rand() .' output from the server Third Msg.</p>';
echo json_encode($arr);
而在Js端:
function myCall() {
var request = $.ajax({
url: "ajax.php",
type: "GET",
dataType: "json"
});
request.done(function(data) {
$("#mybox1").html(data.msg1);
$("#mybox2").html(data.msg2);
$("#mybox3").html(data.msg3);
});
request.fail(function(jqXHR, textStatus) {
alert("Request failed: " + textStatus);
});
}
把你的js代码在这里,所以让我查 –
它是完全一样的链接: 功能的MyCall(){ \t \t \t \t \t VAR请求= $阿贾克斯({ \t \t \t \t \t \t URL: “ajax.php”, \t \t \t \t \t \t类型:“G ET”,\t \t \t \t \t \t \t \t \t数据类型: “html” 的 \t \t \t \t \t}); \t \t \t \t \t request.done(函数(MSG){ \t \t \t \t \t \t $( “#myBox上”)。HTML(MSG); \t \t \t \t \t \t \t \t}); \t \t \t \t \t request.fail(功能(jqXHR,textStatus){ \t \t \t \t \t \t警报( “请求失败:” + textStatus); \t \t \t \t \t}); \t \t \t \t} – Applecow