Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript PHP未从js文件接收AJAX帖子_Javascript_Php_Ajax_Web - Fatal编程技术网

Javascript PHP未从js文件接收AJAX帖子

Javascript PHP未从js文件接收AJAX帖子,javascript,php,ajax,web,Javascript,Php,Ajax,Web,我已经试着解决这个问题好几个小时了,但找不到任何对我有帮助的答案 这是我的javascript文件中的代码 function sendMovement(cel) { var name = "test"; $.ajax({ type: 'POST', url: '../game.php', data: { 'Name': name }, success: function(response) { con

我已经试着解决这个问题好几个小时了,但找不到任何对我有帮助的答案

这是我的javascript文件中的代码

function sendMovement(cel) {
  var name = "test";
  $.ajax({
      type: 'POST',
      url: '../game.php',
      data: { 'Name': name },
      success: function(response) {
          console.log("sent");
      }
  });
}
这是我的PHP文件中的代码(在js文件之外)

调试时,我可以看到AJAX正在发送POST,它确实在控制台“SENT”中打印,但不打印$data


更新:函数console_log()存在于我的PHP文件中,它可以工作

尝试获取JSON格式的响应,因为您的js应该具有如下所示的数据类型:'JSON'

JS代码:-

function sendMovement(cel) {
  var name = "test";
  $.ajax({
      type: 'POST',
      dataType:'JSON',   //added this it to expect data response in JSON format
      url: '../game.php',
      data: { 'Name': name },
      success: function(response) {
          //logging the name from response
          console.log(response.Name);
      }
  });
}
在当前的服务器端代码中,您没有响应或返回任何内容,因此ajax响应中不会显示任何内容

php服务器代码中的更改:-

if($_SERVER["REQUEST_METHOD"] == "POST") {
  
  $response = array();
  $response['Name'] = $_POST['Name'];
  //sending the response in JSON format
  echo json_encode($response);

}

我通过执行以下操作修复了它:

我在my game.php中添加了以下HTML代码(用于调试)

在我的custom.js中,我更新了

function sendMovement(cel) {
  var handle = document.getElementById('response');
  var info = [gameID, cel.id, current_player];

  $.ajax({
    type: 'POST',
    url: '../game.php',
    data: {
      gameID: info[0],
      coord: info[1],
      player: info[2]
    },
    success: function(data) {
      handle.innerHTML = data;
    },
    error: function (jqXHR) {
      handle.innerText = 'Error: ' + jqXHR.status;
    }
  });
}

尝试使用
数据:{Name:Name},
&让我知道?尝试
var\u dump($\u请求)
外部if,并检查是否收到请求
<p style = "color: white;" id="response"></p>
if($_SERVER["REQUEST_METHOD"] == "POST") {
  
  $gameID = $_POST['gameID'];
  $coord = $_POST['coord'];
  $player = $_POST['player'];

  echo "gameID: " . $gameID . "\nCoord: " . $coord . "\nPlayer: " . $player;

}
function sendMovement(cel) {
  var handle = document.getElementById('response');
  var info = [gameID, cel.id, current_player];

  $.ajax({
    type: 'POST',
    url: '../game.php',
    data: {
      gameID: info[0],
      coord: info[1],
      player: info[2]
    },
    success: function(data) {
      handle.innerHTML = data;
    },
    error: function (jqXHR) {
      handle.innerText = 'Error: ' + jqXHR.status;
    }
  });
}