weixin_33676492 2015-07-30 06:08 采纳率: 0%
浏览 21

Ajax不向php发送信息

I'm trying to get some information from my php code when clicking on a button, but it doesn't connect to php.

front page is displayed in index.php
index.php:

    <!DOCTYPE html>
    <html>
    <head>
    <link rel="stylesheet" type="text/css" href="mystyle.css">
    <script type="text/javascript" src="jquery-1.4.4.min.js"></script>
    <script type="text/javascript" src="functions.js"></script>

    <title>Account Panel</title>
    </head>


    <div "getInfos">
    <h2>In this section you can get your inforrmation</h2>
    <button id="getNameBtn">Get Your Name!</button>
    <span id="getNameSpan"> something must come here</span>

    </div>
    </body>
    </html>

javascript codes and ajax are in functions.js:

    $(document).ready(function(){
    $("#getNameBtn").live('click', function() {
    $.ajax({ 
        type: 'POST',
        url: 'handler.php',
        data:JSON.stringify({taskid = 1}),
        headers: {
            'content-type': 'application/json'
        },

        success: function(response) {
            document.getElementById('getNameSpan').innerHTML = response;
        },
        error: function() {
            alert("Error Ajaxing");
        }

        });

   });

and php in serverside is some simple thing like this:

handler.php:

    <?php

    echo('Ajax successful!');

    ?>
  • 写回答

5条回答 默认 最新

  • python小菜 2015-07-30 06:17
    关注

    You have not close the document ready function:

        $(document).ready(function(){
            $("#getNameBtn").live('click', function() {
                $.ajax({ 
                  type: 'POST',
                  url: 'handler.php',
                  data:JSON.stringify({taskid = 1}),
                  headers: {
                    'content-type': 'application/json'
                  },
    
                  success: function(response) {
                    document.getElementById('getNameSpan').innerHTML = response;
                  },
                  error: function() {
                    alert("Error Ajaxing");
                  }
                });
    
           });
    });
    
    评论

报告相同问题?