douju5933 2018-01-18 05:34
浏览 164
已采纳

成功的Ajax jQuery如何获取PHP传递的结果值

How I can get the success (data) value on Ajax? I want to get the value of firstname and lastname, which are Kevin Yam.

Example I want to set input value, document.getElementById("reply").value = "Kevin Yam"

Ajax code:

$.ajax({
    type:'post',
    url:'getuser_reply_name.php',
    data:{get_reply_postid:post_id,get_reply_commentid:comment_id},
    cache:false,
    success: function (data){
        console.log(data);
    }
});

getuser_reply_name.php code:

if($stmt = $conn->prepare($query)) {

    $stmt->execute();

    $stmt->bind_result($user_firstname, $user_lastname);

        while ($stmt->fetch()) {

            $userdetails = array(
                'u_firstname' => $user_firstname,
                'u_lastname' => $user_lastname
            );

            header('Content-Type: application/json');
            echo json_encode($userdetails);
        }
    $stmt->close();
}

Console log result:

enter image description here

  • 写回答

2条回答 默认 最新

  • dsqdpn31467 2018-01-18 05:38
    关注

    1.Add dataType:"JSON", in your $.ajax, so that it automatically parse json data coming from php.

    2.Inside success fetch data from response using keys(what you are seeing in console.log())

    Do like below:-

    $.ajax({
        type:'post',
        url:'getuser_reply_name.php',
        data:{get_reply_postid:post_id,get_reply_commentid:comment_id},
        dataType:'json', //add dataType
        cache:false,
        success: function (data){
            $("#reply").val(data.u_firstname+' '+data.u_lastname); // put value to input box
        }
    
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Java中消息和缓存如何使用
  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路