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");
                  }
                });
    
           });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口