PHP AJAX没有通过$ _POST接收到数据

时间:2018-06-20 11:36:22

标签: php ajax post canvas saving-data

如上所述,我的$ _POST没有收到任何数据。我试图将多个画布dataURLs传递到服务器以存储为图像。数据存在于客户端上,但永远不会到达服务器。它保存了一堆空的.PNG,这向我显示了代码正在执行,那里没有任何数据。

以下是我的JavaScript文件中的相关信息:

function saveImages(){
   $.ajax({
      type: "POST",
      contentType: "application/json; charset=utf-8",
      url: "urlForPHP.php",
      data: $('#form1').serialize(),
      success: function (result) {
        console.log("Success");
      }
    });
}

这是我的PHP文件(仅用于一张图像,为了便于阅读,我删除了存储多个图像的循环)

<?php

  ini_set('display_errors', 'On');
  error_reporting(E_ALL | E_STRICT);

  $rest_json = file_get_contents("php://input");
  $_POST = json_decode($rest_json, true);

  $finalDest = "path/to/save/";
  if (!is_dir($finalDest)) {
    // dir doesn't exist, make it
    mkdir($finalDest, 0777, true);
  }

  //save data
  $file = base64_decode($_POST["image"]);
  $success = file_put_contents($finalDest . $x . ".png", $file);
  print $success ? $file : 'Unable to save the file.';

在此先感谢您,如果之前已经解决过,对不起,但是我已经做了大量的阅读工作,并尝试了类似答案的解决方案,但到目前为止没有任何效果。

编辑

添加了HTML代码,忘记包含我的表单!

<form method="post" accept-charset="utf-8" name="form1">
   <input name="boxTypeID" id="boxTypeID" type="hidden"/>
   <input name="image" id="image" type="hidden"/>
   <input type="button" name="send" onclick="saveImages()"/>
</form>

0 个答案:

没有答案
相关问题