ajax POST响应为空

时间:2018-11-25 11:18:28

标签: jquery ajax

尝试通过单击按钮发布数据,但作为响应,PHP脚本中将其显示为空

这是html    <input type="button" id="btn" value="View Menu" style="padding: 5px 10px; background-color:green;"/>

这是jquery

var action = 1;
$("input").on("click", sendData);
function sendData() {
if ( action == 1 ) {
    $("#btn").css("background", "red");
    var getData = 1;
        $.post({
          url: "drain.php",
          type: "POST",
          data: {"getData" : getData}
        });
    action = 2;
} else {
    $("#btn").css("background", "green");
    var getData = 0;
        $.post({
          url: "drain.php",
          type: "POST",
          data: {"getData" : getData}
        });
    action = 1;
    }
}

这是我试图接收数据(getData)的PHP脚本

$rcvd_data=filter_input_array(INPUT_GET,[
    'data'=>FILTER_SANITIZE_STRING,
    'dev_id'=>FILTER_SANITIZE_STRING
]);

if($rcvd_data !== FALSE && !is_null($rcvd_data)) {
    $fp=fopen('drain.txt', 'a+');

    fwrite($fp, implode(', ', [
        0=>date('Y-m-d h:i:s A', time()),
        1=>$rcvd_data['data'],
        2=>$rcvd_data['dev_id']
    ]) . PHP_EOL);  
}

    $getData = $_POST['getData']; // here the posted data is recieved
    echo "@".$getData."#";
    die();

0 个答案:

没有答案