我无法获得我的ajax请求

时间:2015-11-28 23:53:36

标签: javascript php jquery html ajax

我无法使以下AJAX请求正常工作:

我想让在字段中输入的文字显示在" p"标签

服务器:Apache 2.2 PHP 5

我的HTML:



<html>

<head>

  <title>Test for reponder</title>
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

  <script>
    $function send() {
        $('body').on('click', 'button', function(event) {
              var namee = document.getElementById('name').value;
              //var dataString='name ='+ name;
              $.ajax({
                type: 'POST',
                url: 'responder.php',
                data: namee,
                success: function(html) {
                  $('#msg').html(html);
                }
              })
  </script>
</head>

<body>
  <input type="text" id="name">
  <!--<input type="submit" id="button" value="Send" onclick="return send();">-->
  <button type="button">Send</button>

  <p id="msg"></p>

</body>

</html>
&#13;
&#13;
&#13;

我的PHP文件:

&#13;
&#13;
<?php 
$name=$_POST[ 'name']; 
echo "response:" . $name; 
?>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您可以这样做:

<强> HTML

<input type="text" id="name">
<button type="button">Send</button>

<p id="msg"></p>

<强>的jQuery

<script>
    $(document).ready(function(){
        $(document).on('click', 'button', function(){
            var name = $("#name").val();
            var param = {name: name};

            $.ajax({
                type: 'POST',
                url: 'responder.php',
                cache: 'false',
                data: param,

                beforeSend: function(){
                    // before send
                },

                success: function(data){
                    // success
                    $('#msg').html(data);
                },

                error: function(){
                    // error
                }
            });
        });
    });
 </script>

<强> responder.php

<?php

    if(isset($_POST['name'])){
        $name=$_POST['name']; 
        echo "response:" . $name; 
    }

?>