Home > Backend Development > PHP Tutorial > jquery ajax返回二维数组,在线等,

jquery ajax返回二维数组,在线等,

WBOY
Release: 2016-06-23 13:59:22
Original
1109 people have browsed it

下面是我返回一字符串的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')));
Copy after login


alert(result[0][a]);
Copy after login

没测试,应该是这样的

nbsp;html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



php+jquery+ajax+json示例
<script></script> 

<script></script> 
<script> <br /> <br /> /* <br /> //添加文档 <br /> $(document).ready( <br /> function(){ <br /> //$("body").append('<input type="button" value="请求服务器" id="btn" />'); <br /> $('#btn').click(function (){ getServerData(); }); <br /> $("body").append('<div id="result">'); <br /> } <br /> ); <br /> */ <br /> //getServerData(); <br /> //设置ajax的参数 <br /> function getServerData(){ <br /> $.ajax({ <br /> url:"ajax_server2.php", <br /> data:"area=2", <br /> timeout:3000, <br /> type:"POST", <br /> error:function (XMLHttpRequest, textStatus, errorThrown) {alert('请求错误...'+errorThrown);}, <br /> beforeSend:function (XMLHttpRequest) {alert('发送请求之前...');}, <br /> success: function (data, textStatus) {alert('请求成功...');}, <br /> complete:handleResponse <br /> }); <br /> } <br /> <br /> //成功返回的绑定函数 <br /> function handleResponse(XMLHttpRequest, textStatus) { <br /> //alert('完成啦...'+XMLHttpRequest); <br /> var myjson = eval(XMLHttpRequest.responseText); <br /> var str=""; <br /> for(var i=0;i<myjson.length;i++){ <br /> str+="姓名:"+myjson[i].mapX+"<br />"; <br /> alert(str); <br /> //str+="年龄:"+myjson[i].age+"<br />"; <br /> //str+="性别:"+myjson[i].sex+"<br />"; <br /> } <br /> <br /> } <br /> <br /> </script>




   









//$data[]=array('name'=>'万','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

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template