Ajax post 在服务器上有空值
Ajax post has empty values on the server
我正在尝试使用从客户端收到的 base64 字符串保存图像文件。
所以我有这个 ajax post:
$.ajax({type: "POST", url: "upload_post.php", data: postData, dataType: "text", success: function(result){
alert("post result: " + result + " - data:" + postData);
location.reload();
}});
这里有一个 postData
的例子(我知道它包含数据):
{"ship_id":"407","base64_upload":"ABCSFSAFGDGFA....."}
现在这是我的 php 代码来处理这个 post:
$id = $_POST['ship_id'];
$img = $_POST['base64_upload'];
define('UPLOAD_DIR', 'news.site.com/docs/'.$id.'/');
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file. '.$file.'';
问题是 $_POST 变量总是空的。这是为什么? json相关? location.reload() 相关?我该如何解决?
编辑
通过对 ajax 数据执行 JSON.parse(postData)
,我已经用实际数据 post 编辑了这些变量。现在我的问题是我仍然无法保存图像文件。有帮助吗?
这就是我为 jQuery Post 所做的。希望对你有帮助
$.post("upload_post.php",
{
ship_id: "407",
base64_upload: "ABCSFSAFGDGFA....."
},
function(data, status){
alert("post result: " + status+ " - data:" + data);
location.reload();
});
你在php中获取的数据完全一样。
我再一次回答我自己的问题。
问题是我有 define('UPLOAD_DIR', 'news.site.com/docs/'.$id.'/');
然后 $file = UPLOAD_DIR . uniqid() . '.png';
而这个 UPLOAD_DIR
目录实际上并不存在。所以我添加了一个检查它是否存在然后在创建文件之前创建目录:
$id = $_POST['ship_id'];
$img = $_POST['base64_upload'];
define('UPLOAD_DIR', 'news.site.com/docs/'.$id.'/');
$file2 = UPLOAD_DIR;
if(!file_exists($file2)){
mkdir($file2, 0777, true);
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file. '.$file.'';
}
我正在尝试使用从客户端收到的 base64 字符串保存图像文件。
所以我有这个 ajax post:
$.ajax({type: "POST", url: "upload_post.php", data: postData, dataType: "text", success: function(result){
alert("post result: " + result + " - data:" + postData);
location.reload();
}});
这里有一个 postData
的例子(我知道它包含数据):
{"ship_id":"407","base64_upload":"ABCSFSAFGDGFA....."}
现在这是我的 php 代码来处理这个 post:
$id = $_POST['ship_id'];
$img = $_POST['base64_upload'];
define('UPLOAD_DIR', 'news.site.com/docs/'.$id.'/');
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file. '.$file.'';
问题是 $_POST 变量总是空的。这是为什么? json相关? location.reload() 相关?我该如何解决?
编辑
通过对 ajax 数据执行 JSON.parse(postData)
,我已经用实际数据 post 编辑了这些变量。现在我的问题是我仍然无法保存图像文件。有帮助吗?
这就是我为 jQuery Post 所做的。希望对你有帮助
$.post("upload_post.php",
{
ship_id: "407",
base64_upload: "ABCSFSAFGDGFA....."
},
function(data, status){
alert("post result: " + status+ " - data:" + data);
location.reload();
});
你在php中获取的数据完全一样。
我再一次回答我自己的问题。
问题是我有 define('UPLOAD_DIR', 'news.site.com/docs/'.$id.'/');
然后 $file = UPLOAD_DIR . uniqid() . '.png';
而这个 UPLOAD_DIR
目录实际上并不存在。所以我添加了一个检查它是否存在然后在创建文件之前创建目录:
$id = $_POST['ship_id'];
$img = $_POST['base64_upload'];
define('UPLOAD_DIR', 'news.site.com/docs/'.$id.'/');
$file2 = UPLOAD_DIR;
if(!file_exists($file2)){
mkdir($file2, 0777, true);
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file. '.$file.'';
}