Home  >  Article  >  Backend Development  >  ajax返回数组,页面接收不到数据

ajax返回数组,页面接收不到数据

WBOY
WBOYOriginal
2016-06-06 20:21:091378browse

后端代码

<code>$sql=mysql_query("select number,decode from hongbao order by rand() limit 1 ");
$row=mysql_fetch_array($sql);
$_SESSION['ip']=$_POST['ip'];
$data['number']=$row['number'];
$data['decode']=$row['decode'];
echo  json_encode($data);
?></code>

前端代码

<code>$(document).ready(function(){
    $(".red").click(function(){
        $(this).addClass("shake");
        setTimeout(function(){
            $(".red").removeClass("shake");
            $.post(
                  "recive.php",
                        {ip:$("#ip").val()},
                        function(data){
                            $(".windows").fadeIn();
                            $("#number").attr("value",dada.number);
                            $("#decode").attr("value",data.decode);
                            $(".opacity").fadeIn();
                        }
            )
        },2000);
    });
    $(".close").click(function(){$(this).parent().fadeOut();$(".opacity").fadeOut()})
});</code>

返回数据那里是不是写错了,求大神分析···

回复内容:

后端代码

<code>$sql=mysql_query("select number,decode from hongbao order by rand() limit 1 ");
$row=mysql_fetch_array($sql);
$_SESSION['ip']=$_POST['ip'];
$data['number']=$row['number'];
$data['decode']=$row['decode'];
echo  json_encode($data);
?></code>

前端代码

<code>$(document).ready(function(){
    $(".red").click(function(){
        $(this).addClass("shake");
        setTimeout(function(){
            $(".red").removeClass("shake");
            $.post(
                  "recive.php",
                        {ip:$("#ip").val()},
                        function(data){
                            $(".windows").fadeIn();
                            $("#number").attr("value",dada.number);
                            $("#decode").attr("value",data.decode);
                            $(".opacity").fadeIn();
                        }
            )
        },2000);
    });
    $(".close").click(function(){$(this).parent().fadeOut();$(".opacity").fadeOut()})
});</code>

返回数据那里是不是写错了,求大神分析···

<code>$data=array();
$result=mysql_query("SELECT xx FROM xxoo LIMIT 1");
//把结果放进循环中看看
while($row=mysql_fetch_array($result)){
    array_push($data,array("xx"=>$row['xx']));
    //还是不行可能是json的格式不是标准的
    //以上的标准的json格式
}
echo json_encode($data);</code>

怀疑是你的url写错了,是 recive.php 还是 receive.php ?

$.post少了一个参数,设置为 json

Statement:
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