Php没有通过$ _POST接收变量

问题描述 投票:-1回答:2

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);
javascript php jquery ajax post
2个回答
0
投票

data: imgPath应该是data: {imgPath: imgPath}你应该在php后端改变你的$_GETs到$_POSTs


0
投票

感谢@Reflective的帮助。在研究了img_delete.php和google搜索中的数据之后,我找到了一个解决方案。出于某种原因,在我看来,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);
        }
    });

});
© www.soinside.com 2019 - 2024. All rights reserved.