鍍金池/ 問答/HTML/ 為何 $GET無(wú)法收到AJAX .get方法提交的數(shù)據(jù)?

為何 $GET無(wú)法收到AJAX .get方法提交的數(shù)據(jù)?

客戶端的代碼
send.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    
<script src="http://127.0.0.1/jquery-3.3.1.js"></script>
<script>
function Ajax(){
    $.get("receive.php",
        {user:$("#user").val(),comment:$("#comment").val()},
        function(data){
            $("#target").append(data);
        });
    };
</script>
<p>user:<input id="user" type="text"></p>
<p>comment<textarea id="comment" name="" cols="20" rows="2"></textarea></p>
<input type="button" value="Ajax submit" onclick="Ajax()">
<div id="target"></div>

</body>
</html>

receive.php

<?php
header("Content-type:text/html;charset=utf-8");
$user = $_GET['user'];
$comment = $_GET['comment'];
echo "<h3>user:" . $user . "</h3>";
echo "<p>comment:" . $comment . "</p>";
?>

圖片描述
圖片描述
客戶端發(fā)送數(shù)據(jù)后,我打開127.0.0.1/receive.php
顯示結(jié)果是

user:
comment:

為何不是:

user:john
comment:1234

回答
編輯回答
荒城

可以啊

clipboard.png

2017年8月22日 23:55
編輯回答
紓惘

呵呵$.get里面的第二個(gè)參數(shù)是回調(diào)的,要用$.get必須放到url的參數(shù)中,建議用$.ajax

$.ajax({
    url:'receive.php',
    data:{user:$("#user").val(),comment:$("#comment").val()},
    type:"get",
    success:function(data){
        $("#target").append(data);
    }
});
2018年3月19日 05:38