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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失