AJAX调用返回PHP值为空

问题描述 投票:0回答:1

我有一个关于ajax调用的问题,我不明白为什么我仍然得到这个空的成功电话。我试过过去的解决方案来解决这个问题,但我不知道是什么原因引起的。

正如你可以看到我的表,然后当我点击更新,删除或添加,甚至print_r($ _ POST)的操作时,它仍然是空的。 enter image description here enter image description here然后当我去控制台时我得到了这个错误。 enter image description here选择的attr的值仍然发送到php文件

继承我的代码:

 $(document).on('click', '.update', function(){
    var user_ID = $(this).attr('id');

$.ajax({
  url:"datatable/account/fetch_single.php",
  method:"POST",
  data:{user_ID:user_ID},
  dataType:"json",
  success:function(data)
  {
    console.log(data);

    $('#account_modal').modal('show');
    $('#username').prop("disabled", true);
    $('#username').val(data.user_Name);
    $('#email').val(data.user_Email);
    $('#pass').val(data.user_Pass);
    $('#con_pass').val(data.user_Pass);
    $('#level').val(data.level_ID).change();
    $('#status').val(data.user_status).change();
    $('#action').val('Edit');
    $('#operation').val('Edit');
    $('.modal-title').text('Edit Account Info');
    $('#user_ID').val(user_ID);
  }
})
  });

fetch_single.php

include('db.php');
include('function.php');
if(isset($_POST["user_ID"]))
{
    $output = array();
    $statement = $conn->prepare(
        "SELECT * FROM `user_accounts`
        WHERE user_ID = '".$_POST["user_ID"]."' 
        LIMIT 1"
    );
    $statement->execute();
    $result = $statement->fetchAll();
    foreach($result as $row)
    {

        $output["level_ID"] = $row["level_ID"];
        $output["user_Name"] = $row["user_Name"];
        $output["user_Pass"] = decryptIt($row["user_Pass"]);
        $output["user_Email"] = $row["user_Email"];
        $output["user_status"] = $row["user_status"];

    }
    echo json_encode($output);
}

或者它可能是问题的原因是设计模板?

php ajax datatables
1个回答
1
投票

我解决了这个问题,在我要求的php文件中,如果您在ajax中的数据看起来像我将$_POST更改为$_REQUEST

data:{user_ID:user_ID}  

要么

data: "fname="+fname+"&lname="+lname

这可能适合你,那么如果你的数据看起来像这样data:new FormData(this)也许你应该使用这种类型

 data:{user_ID:user_ID}

处理请求并执行您的SQL。


但我得到的最好的解决方案是改变你的

方法: “POST”

键入: “POST”

解决这个问题。


如果你的data.success_variable是未定义的添加

header('Content-type:  application/json.'); 

到你要求的php文件。

enter image description here

相关主题:ppumkin5回答JQuery Ajax is sending GET instead of POST

© www.soinside.com 2019 - 2024. All rights reserved.