php 如何在jquery数据表的ajax调用中发布参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25505755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:51:47  来源:igfitidea点击:

How to post the parameter in ajax call of jquery datatable

phpjqueryajaxdatatables

提问by chaya

As of now I am passing parameter along with URL in ajax call of data table.

截至目前,我正在数据表的ajax调用中传递参数和URL。

But I want to pass it as POSTmethod, please anyone one help me regarding parameter passing in post method, here's my trial code:

但我想将它作为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+''
});

回答by Kevin

Just pass it like a normal jQuery ajax in POST fashion.

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

The structure should look like this:

结构应如下所示:

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

Example:

例子:

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

In PHP, just access the POST indices as usual (just the straightforward approach):

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

getResult.php

getResult.php

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

DataTables manual entry

数据表手动输入

回答by bharat

You can try this way:

你可以试试这个方法:

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

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

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

回答by Abdullah Sheikh

$("#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
                } );
            } });

This is real time example.The aoData contains all the parameters which is required on server side and you can also push your own custom parameters

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