Ajax没有向php发送信息

Ajax not sending info to php

本文关键字:信息 php Ajax      更新时间:2023-09-26

我想从我的php代码中获得一些信息时,点击一个按钮,但它不连接到php。

首页显示在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代码和ajax都在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");
        }
        });
   });

和PHP在服务器端是一些简单的东西,像这样:

handler.php:

    <?php
    echo('Ajax successful!');
    ?>

您还没有关闭文档准备功能:

    $(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");
              }
            });
       });
});

data:JSON.stringify({taskid = 1}),

承担是

data:JSON.stringify({taskid: 1}),

首先,您最好使用较新的jquery版本。

你的代码中至少有一个错误:

data:JSON.stringify({taskid = 1})

json应该是

{taskid : 1}

使用冒号,而不是等号。不确定这是否适用于您的jQuery版本,但通常数据已经可以作为json对象附加,因此整行应该这样工作:

data: {taskid : 1},

然后数据在PHP页面中作为POST数据可见。请注意,live()函数自1.7起已弃用。你可以使用

$("#getNameBtn").click(function(){...});

。此外,我认为你不需要在你的请求头

您需要做的第一个重要更改是使用$.on而不是$.live,因为后者已被弃用。你应该检查的另一件事是,handler.php文件是否与JS/HTML文件处于同一级别。可能是您的代码无法访问该文件。下面是你可以尝试的:

$(document).ready(function(){
    $("#getNameBtn").on('click', function() {
        $.ajax({ 
            type: 'POST',
            url: 'handler.php',
            data: { call: 'myAjax', taskid : 1 },
            headers: {
                'content-type': 'application/json'
            },
            success: function(response) {
                $('#getNameSpan').html(response);
            },
            error: function() {
                alert("Error Ajaxing");
            }
        });
    });
});

在PHP文件中,您可以检查call键:

<?php
    if(isset($_POST) && $_POST['call'] == 'myAjax') {
        echo $_POST['taskid'];
        exit;
    }
?>

那个exit真的很重要。

在返回JSON的PHP文件中,您也应该将标题设置为JSON。

header("Content-Type: application/json"); //Above all HTML and returns

你的问题的真正答案已经有答案了。