Php不会通过$ _POST接收变量。我试图用ajax向php页面传递一个变量,但是php接受一个变量为NULL。告诉我,错误是什么,以及如何修复它?
jquery代码:
var imgPath;
$(".close_modal_clear_cover").on("click", function(e) {
imgPath = $("#cover_preview").attr('src');
$.ajax({
url: "/management/ajax_scripts/img_delete.php",
type: "POST",
data: imgPath,
async: true,
cache: false,
contentType: false,
dataType: "json",
processData: false,
success: function (returndata) {
console.log(imgPath); //url of image
console.log(returndata); // NULL
}
});
});
img_delete.php代码:
if (isset($_POST['imgPath'])) {
$path= $_POST['imgPath'];
unlink($path);
$response = $path;
} else {
$response = "false";
}
echo json_encode($response);
发布于 2018-06-09 18:23:22
感谢@Reflective的帮助。在研究了img_delete.php中的数据并搜索了一下之后,我找到了一个解决方案。由于某种原因,contentType被指定为false,但它是必需的'application / x-www-form-urlencoded;charset = UTF-8‘。
这是一个愚蠢的错误,但突然有人与相同的碰撞。
$(".close_modal_clear_cover").on("click", function(e) {
// imgPath = $.session.get("imgCover");
imgPath = JSON.stringify($("#cover_prewiew").attr('src'));
$.ajax({
url: "/management/ajax_scripts/img_delete.php",
type: "POST",
data: {imgPath: imgPath},
async: true,
cache: true,
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
dataType: "json",
processData: true,
success: function (returndata) {
console.log(imgPath);
console.log(returndata);
}
});
});
发布于 2018-06-09 06:21:56
data: imgPath
应该是data: {imgPath: imgPath}
,并且你应该在php后端把你的$_GET
s改成$_POST
s
https://stackoverflow.com/questions/50769034
复制相似问题