【问题标题】:How to post the parameter in ajax call of jquery datatable如何在jquery数据表的ajax调用中发布参数
【发布时间】:2014-10-19 18:35:58
【问题描述】:

到目前为止,我在数据表的 ajax 调用中将参数与 URL 一起传递。

但我想将它作为POST 方法传递,请任何人帮助我关于在 post 方法中传递参数,这是我的试用代码:

// Sending through GET
var $table = $('#example').dataTable( 
    "processing": true,
    "serverSide": true,
    "bDestroy": true,
    "bJQueryUI": true,
    "ajax": 'getResult.php?formName=afscpMcn&action=search&mcn_no='+mcnNum+'&cust_nm='+cust_num+'&emp_id='+emp+''
});

【问题讨论】:

标签: php jquery ajax datatables


【解决方案1】:

只需像普通的 jQuery ajax 一样以 POST 方式传递它。

结构应该是这样的:

ajax: { type: 'POST', url: <path>, data: { your desired data } }

例子:

var $table = $('#example').dataTable( 
    "processing": true,
    "serverSide": true,
    "bDestroy": true,
    "bJQueryUI": true,
    "ajax": {
        'type': 'POST',
        'url': 'getResult.php',
        'data': {
           formName: 'afscpMcn',
           action: 'search',
           // etc..
        },
    }
});

在 PHP 中,像往常一样访问 POST 索引(只是简单的方法):

getResult.php

$form_name = $_POST['formName'];
// the rest of your values ...

DataTables manual entry

【讨论】:

  • 嘿凯文,当 json 返回 0 结果时我遇到了麻烦,想象一下如果表没有条目......所以当它返回一个空白 JSON 时,我从数据表中收到错误......这是一个通知 JSON 格式错误的默认警报.. 你知道我该如何处理吗?
【解决方案2】:
$("#tbl").dataTable({
            oLanguage: {
                sProcessing: '<div id="loader"></div>'
            },
            bProcessing: true,
            "bServerSide": true,
            "iDisplayLength": pageSize,
            "sAjaxSource": " /PurchaseOrder/AddVendorItems", // url getData.php etc
            "fnServerData": function ( sSource, aoData, fnCallback, oSettings ) {
                aoData.push({ "name": "where", "value": ID +" AND ISNULL(IsFinal,0) = "+ ($("#chkFinal").bootstrapSwitch('state') == true ? 1 : 0) });
                aoData.push({"name": "PackIDFK", "value": $("#PackIDFK").val()}) //pushing custom parameters
                oSettings.jqXHR = $.ajax( {
                    "dataType": 'json',
                    "type": "POST",
                    "url": sSource,
                    "data": aoData,
                    "success": fnCallback
                } );
            } });

这是实时示例。aoData 包含服务器端所需的所有参数,您也可以推送自己的自定义参数

【讨论】:

    【解决方案3】:

    你可以试试这个方法:

    $('#example').dataTable( {
      "ajax": {
        "url": "data.json",
        "data": function ( d ) {
            d.extra_search = $('#extra').val();
        }
      }
    });
    

    https://datatables.net/reference/option/ajax.data

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-30
      • 1970-01-01
      • 2016-07-22
      • 2021-08-28
      • 2013-06-17
      • 2021-01-17
      • 1970-01-01
      相关资源
      最近更新 更多