jQueryAJAX没有得到PHP的响应

jQuery AJAX gets no response from PHP

本文关键字:PHP 响应 jQueryAJAX      更新时间:2023-09-26

当我尝试在PHP中发出jQueryAJAX请求时,我遇到了几个问题。我的简单例子有什么问题?

index.php-加载JS、PHP并定义一个按钮和一个段落。

<html>
    <head>
        <script src='jquery-3.0.0.js'></script>
        <script src='main.js'></script>     
    </head>
    <body>
        <button id="action" onClick="Send()">Send data</button>
        <p id="result"></p>
        <?php require('main.php'); ?>                   
    </body>
</html>

main.js-它包含与"onClick"事件关联的函数。

function Send(){        
    $.ajax({
        url: 'main.php',
        type: 'POST',
        data: {
            input: "test",
            message: "Sending..."
        },
        contentType: 'application/json',
        success: function(data) {               
            alert(data);                
            document.getElementById("result").innerHTML = "DONE!";          
        }       
    }); 
} 

main.php-它监听POST请求,并发回一个JSON。

<?php
if ($_POST){        
    // Make a array with the values
    $vals = [
        'input'     => $input,
        'message'   => $message
    ];
    echo json_encode($vals);
}

success回调运行,但alert消息完全为空。CCD_ 5表示CCD_。

很明显,没有任何数据被发送回AJAX请求。我该怎么修?

这是因为您没有以JSON的形式从PHP发送响应。

echo json_encode()上方添加以下行;

header('Content-Type: application/json');

所以你的PHP代码看起来像这样,

<?php
if ($_POST){        
    // Make a array with the values
    $vals = array(
        'input'     => $input,
        'message'   => $message
    );
    header('Content-Type: application/json');      
    echo json_encode($vals, JSON_PRETTY_PRINT);     // Now we want to JSON encode these values to send them to $.ajax success.
    exit;                                           // to make sure you aren't getting nothing else
}
?>

同样正如@Ismail提到的dataType : 'json'.AJAX调用中添加此内容以接受来自API的JSON响应。

在main_js.js 中

function Send(){
    var data_JSON = {
        input: "test",
        message: "Sending..."
    };          
    $.ajax({
        url: 'main_php.php',
        type: 'POST',
        data: data_JSON,
        dataType: 'json',
        success: function(response){                
            if(response.type == "success")               
            {
                 alert(JSON.stringify(response.data));
                 alert(response.data.input);
                 document.getElementById("result").innerHTML =  response.message;          
            }
            else
            {
                 document.getElementById("result").innerHTML = response.message;          
            }
        }       
    }); 
} 

在php代码中

<?php
$response= array();
if (isset($_POST) && !empty($_POST)){        
    // Make a array with the values
    $vals = $_POST;      
    $response['data']=json_encode($vals);     // Now we want to JSON     
    $response["type"]="success";
    $response["message"]="Successfully posted";
}
else
{
    $response=array(
       "type"=>"error",
       "message"=>"invalid post method"
    );
}
ob_clean();
echo json_encode($response);