下面是我返回一字符串的ajax页面
a页面
$(document).ready(function(){
$(#1).keyup(function(){
$.post(action.php,{n1:$(#1).val(),n2:$(#1).val()},function(result){
alert(result)
});
});
});
action.php页面
$txt=$_post['n1'];
echo 'this sw from page b, input is '.$txt.$_post['n2'];
?>
现问:现需通过action.php页面得到一个php二维数组,在a页面可alert出一个js二维数组,应该如何实现呀,(解答时可用测试数据)thanks
回复讨论(解决方案) alert(二维数组) 的话结果会是 array
可以返回json格式数据。
与js的交互以后都尽量的采用json数据格式.
php 可以直接使用函数输出这个json内容.
$(document).ready(function(){
$(#1).keyup(function(){
$.post(action.php,{n1:$(#1).val(),n2:$(#1).val()},function(result){
alert(result.n1+' '+result.n2);
},'json');
});
});
action.php页面
$txt=$_post['n1'];
echo json_encoding(array('n1'=>$_post['n1'],'n2'=>$_post['n2']));
exit;
?>
echo json_encode(array(array('a'=>'b'),array('c'=>'d')));
alert(result[0][a]);
没测试,应该是这样的
nbsp;html public -//w3c//dtd xhtml 1.0 transitional//en http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd>
php+jquery+ajax+json示例
'万','sex'=>'男','age'=>12);
//$data[]=array('name'=>'陈','sex'=>'女','age'=>10);
/*
$data[]=array('name'=>$_request['name'],'sex'=>$_request['sex'],'age'=>$_request['age']);
echo json_encode($data);
*/
require_once('../ini.php');
$area=$_request['area'];
if($area==2)
{
$area=东湖;
}
$sql=select b.estatename,b.mapx,b.mapy
from zh_estate b left join zh_estate_pic p on b.estateid=p.estateid
where 1 and (b.mapx is not null and b.mapx !='' );
if($price)
{
$price = addslashes(trim($price));
$price = explode(,, $price);
$smallprice = $price[0];
$largeprice = $price[1];
$sql.=' and b.curave between '.$smallprice.' and '.$largeprice;
}
if($area)
{
$sql.= and b.districtname='.$area.';
}
$sql.=' group by b.estateid limit 2 ';
$objdb = dblink();
$objdb->execute($sql);
while($rs_f=$objdb->fetch_array())
{
//iconv('gbk','utf-8',$rs_f['estatename']);
//$rs_f['estatename']=urlencode(iconv('gb2312', 'utf-8',$rs_f['estatename']));
$data_f[]=$rs_f;
}
echo json_encode($data_f);
// return $data_f;
?> jquery+asp.net 后台数据传到前台js进行解析的办法说明 - 项目实战 - it工作生活这点事。just such so!
http://www.suchso.com/projecteactual/jquery-aspnet-back-js-parse-object-list.html