简体   繁体   English

通过datatables ajax请求发送POST数据

[英]Sending POST data through datatables ajax request

I'm trying to make a simple ajax call in datatables that is reliant on a post array of IDs from a previous page's form. 我试图在数据表中进行一个简单的ajax调用,该调用依赖于上一页表格中ID的后置数组。 I am getting the error : 我收到错误消息:

Invalid JSON Response

which tells me that my returned json array is probably empty or something and I have a feeling it has to do with the POST data not being sent to my php/sql external script on which ajax is requesting the data from. 这告诉我返回的json数组可能是空的,或者与POST数据没有发送到我的php / sql外部脚本有关,而ajax从php / sql外部脚本请求数据。

I'm not sure how to test it as I don't know how to include the $_POST data in the URL to my external php page to outright trigger the script. 我不确定如何测试它,因为我不知道如何在外部php页面的URL中包含$_POST数据以完全触发脚本。

Heres my current datatables init and php from the results page: 从结果页面继承我当前的数据表init和php:

<?php
include_once('../functions.php');
sec_session_start();
print_r($_POST['post_id']); <-- making sure the post data made it this far
?>

<script type="text/javascript">  
    $(document).ready(function() {
    var compTable = $('#compTab').dataTable({
        "processing": true,
        "serverSide": true,
        "ajax": {
            "url": "baddebt_ext_sql.php",
            "type": "POST",
            "dataType": 'json',
            "data": {"post_id": $_POST['post_id']}
        },                              
        "Columns": [
            { "data": "provider_num" },
            { "data": "provider_name"},
            { "data": "261_total_bed_debts", "sClass": "rightAlign" },
            { "data": "271_medicare_bad_debts", "sClass": "rightAlign" },
            { "data": "281_non_medicare_bad_debts", "sClass": "rightAlign" },
            { "data": "1_cost_to_charge_ratio", "sClass": "rightAlign" },
            { "data": "291_cost_of_non_mcr_bad_debts", "sClass": "rightAlign" }
        ],
        "scrollY":        "600px",
        "scrollCollapse": true,
        "paging":         false,
        "order": [[ 2, "desc" ]],
        "oLanguage": { "sSearch": "Filter All Fields By:" },
        "Dom": '<"clear">lfrtipT',
        "TableTools": {
              "sSwfPath" : "../php/tabletools/swf/copy_csv_xls_pdf.swf" }
    });

and here is my SQL: 这是我的SQL:

<?php
include_once('../link_costreport_2013.php');
if(isset($_POST['post_id'])){
    $in = $_POST['post_id']; <-- THIS IS WHERE THE POST DATA IS SUPPOSED TO BE RECEIVED
}
$data = array();
foreach ($in as $id){
    $query = $link->prepare("SELECT id,provider_num, provider_name, 261_total_bed_debts, 271_medicare_bad_debts, 281_non_medicare_bad_debts, 1_cost_to_charge_ratio, 291_cost_of_non_mcr_bad_debts
                FROM `s10`
                            WHERE `id` = :id");
    $query->bindParam(':id', $id, PDO::PARAM_INT);
    $query->execute();
    $results = $query->fetch(PDO::FETCH_ASSOC);
    $results['261_total_bed_debts'] = "\$".number_format($results['261_total_bed_debts']);
    $results['271_medicare_bad_debts'] = "\$".number_format($results['271_medicare_bad_debts']);
    $results['281_non_medicare_bad_debts'] = "\$".number_format($results['281_non_medicare_bad_debts']);
    $results['291_cost_of_non_mcr_bad_debts'] = "\$".number_format($results['291_cost_of_non_mcr_bad_debts']);
    $results['provider_name'] = "<a id='".$results['id']."' data-toggle='modal' href='#provmodal' class='push'>".$results['provider_name']."</a>";
    $data[] = $results;
}
echo json_encode($data);

If anyone knows how I can get my json array from this script without utilizing the previous pages $_POST data it is supposed to send, then I will gladly post it as well. 如果有人知道如何从该脚本中获取我的json数组,而又不利用它应该发送的先前页面$ _POST数据,那么我也很乐意将其发布。

Basically I'm just wondering if there are any steps I am missing when it comes to feeding this array of IDs through my datatables ajax query and into the second page's sql. 基本上,我只是想知道在通过我的datatables ajax查询并将该ID数组馈入第二页sql时是否缺少任何步骤。 ( an example of the post_id array is like this: Array ( [0] => 299 [1] => 1555 [2] => 3539 ) ) (post_id数组的示例如下: Array ( [0] => 299 [1] => 1555 [2] => 3539 )

Diagnosing this problem is pretty easy. 诊断此问题非常容易。 Simply right mouse click and "inspect element" then choose the Network tab. 只需右键单击并“检查元素”,然后选择“网络”选项卡。 Toggle the transaction as you would in your interface. 就像在界面中一样切换事务。 You'll see a new network transaction in the network tab. 您会在“网络”标签中看到一个新的网络交易。

Click on that new network transaction -- it should have the address that you're defining in the ajax call. 单击该新的网络事务-它应该具有您在ajax调用中定义的地址。 The headers will show the variables you sent via post, response will show what the server returned. 标头将显示您通过帖子发送的变量,响应将显示服务器返回的内容。

The error is indicating that your response will fail if you paste the payload into jslint.com and evaluate it. 该错误表明如果将有效负载粘贴到jslint.com并进行评估,您的响应将失败。 What the cause of that failure is will require more details than you've provided. 导致此故障的原因将需要比您提供的更多的详细信息。

In the ajax example you use, "data" is the data that's being sent to the server. 在您使用的ajax示例中,“数据”是要发送到服务器的数据。 To send a large amount of data in that post, I'd enclose your inputs in a form tag, then do a $.serialize or $.serializeArray on that form. 要在该帖子中发送大量数据,我会将您的输入包含在一个表单标签中,然后在该表单上执行$ .serialize或$ .serializeArray。 You can then send that serialized data over as a variable via the data attribute, where it will be posted to your server. 然后,您可以通过data属性将该序列化的数据作为变量发送,并将其发布到服务器上。 From there, just deal with the resulting post variables via PHP. 从那里,只需通过PHP处理生成的post变量。

Please do update your code to use 1.10 API variables. 请确实更新代码以使用1.10 API变量。 Support for the old ones will be deprecated in the future versions. 在将来的版本中将不再支持旧版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM