【问题标题】:ajax not sending parameter to scriptajax不向脚本发送参数
【发布时间】:2020-05-28 18:23:19
【问题描述】:

尝试使用 ajax 填充数据表。传递参数如下:

$('#submitDetails').on('click', function()
{
  let details = $('#searchDetails').val();

  $.ajax({
        url: 'api/searchDetailInfo.php',
        type: 'POST',
        data: details,
        dataType: 'html',
        success: function(data, textStatus, jqXHR){
          console.log(data); // <-- printing the return from the php script
          let jsonObject = JSON.parse(data);
          var table = $('#example1').DataTable({    
                "data": jsonObject,
                "columns": [
                        { "data": "COLUMN1" },              
                        { "data": "COLUMN2" },
                        { "data": "COLUMN3" }
                ],
                "iDisplayLength": 50,
                "order": [[ 1, "desc" ]],
                "paging": true,
                "scrollY": 550,
                "scrollX": true,
                "bDestroy": true,
                "stateSave": true,
                "sPaginationType":"full_numbers",
                "autoWidth": true
        },
        error: function(jqHHR, textStatus, errorThrown) {
            $('#loadingDiv').hide();
            $('#errorModal').modal('show');
            $('.message').text('There was an error conducting your search.');
            console.log('fail: '+ errorThrown);
            return false;
        }
});

我正在访问 PHP 脚本,但该脚本没有接收到参数。

<?php
  include("../include/database.php");

  if(isset($_POST['details']))
  {
    echo "good";
  }
  else
  {
    echo "bad";
  }
?>

我只是在控制台中变得“糟糕”。我不确定为什么没有将参数发送到脚本。

我该如何解决这个问题?

【问题讨论】:

  • 你试过print_r($_POST);看看发送了什么。
  • @NigelRen - 测试...
  • 我得到以下信息:数组([new_york] =>)
  • data: details, 更改为 data: {details:details}, 为 post 变量提供一个标识符。
  • 完成 @JohnBeasley 很高兴能提供帮助!

标签: php jquery ajax datatable


【解决方案1】:

您需要将data: details, 更改为data: {details:details},,为post 变量提供一个标识符,使$_POST['details'] 有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-12
    • 2015-06-26
    相关资源
    最近更新 更多