jQuery AJAX没有得到PHP的响应

问题描述:

我曾经能够很好地在NodeJS中完成jQuery AJAX请求,但是当我尝试在PHP中做同样的事情时,我遇到了几个问题。我在SO中找不到任何东西可以提供帮助,所以我想问一下,我的简单示例有什么问题?jQuery AJAX没有得到PHP的响应

index.php - 真的不多,只加载2个JS,1个PHP并定义一个按钮和一个段落。

<html> 
    <head> 
     <script src='jquery-3.0.0.js'></script> 
     <script src='main_js.js'></script>  
    </head> 
    <body> 
     <button id="action" onClick="Send()">Send data</button> 
     <p id="result">Lorem ipsum</p> 

     <?php require('main_php.php'); ?>     
    </body> 
</html> 

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

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

} 

main_php.php - 它侦听POST请求,从理论上说,并发送回一个JSON与echo。再次,从理论上...

<?php 
if ($_POST){   
    // Make a array with the values 
    $vals = array(
     'input'  => $input, 
     'message' => $message 
    );  
    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 
} 
?> 

jQuery的AJAX的success函数运行,如文本 “DONE!”出现在该段落中,但alert消息完全是空的。 alert(data.input)(和message相同)显示undefined

很明显,没有数据发送回AJAX请求。我该如何解决它?

注意:它是整个代码,没有其他显示,我也尽可能缩短和简化。

+0

我敢肯定,这可以帮助你:http://*.com/questions/8050709/ajax-doesnt- get-response-from-php-side?rq = 1 –

+0

不要以为这实际上是你的问题,但是你在两个程序之间传递数据。你不需要JSON_PRETTY_PRINT – RiggsFolly

+0

所以你看看'数据'的内容使用浏览器javascript调试器 – RiggsFolly

这是因为您没有将PHP的响应作为JSON发送。

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响应。

+1

你迟到了13秒:D –

+0

是啊13秒,SO差别很大:D –

+1

我从来不用使用'header()'你建议所以我不认为它真的有必要 – RiggsFolly

在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); 
+0

也可以在alert(data.input)中使用if条件。 –

+0

完成后,它显示“ERROR!”,但不仅当我单击按钮时,还将错误消息打印到HTML的底部。不知道它是否正确。 –

+0

根据你的建议更改答案:)谢谢 –