【问题标题】:AJAX Call Return PHP Value EmptyAJAX 调用返回 PHP 值空
【发布时间】:2019-03-22 18:59:31
【问题描述】:

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

您可以看到我的表格,然后当我单击更新、删除或添加操作时,甚至 print_r($_POST) 它仍然是空的。 然后当我去控制台时,我得到了这个错误。 选择的 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);
}

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

【问题讨论】:

  • 您能否将此处的代码缩小到一个不起作用的特定操作/功能?当可能存在一个主要问题时,要经历很多事情。
  • @PatrickQ 好的先生。我已经编辑了我的问题
  • 您是否使用浏览器的网络检查器查看原始请求和响应?
  • @PatrickQ 试过了,但我不确定我是否正确,是控制台和源附近的选项卡

标签: php ajax datatables


【解决方案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 文件。

相关话题: ppumkin5 回复 JQuery Ajax is sending GET instead of POST

【讨论】:

    猜你喜欢
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-05
    • 1970-01-01
    相关资源
    最近更新 更多