用DataTables Ajax将数据发布到php控制器。

时间:2021-05-25 21:34:56

I'm trying to use the DataTables library for Jquery to POST some simple data to a controller which is a SQL query. This query will return JSON and I will go on my merry way and then do what I will with the data.

我正在尝试使用Jquery的DataTables库向一个名为SQL查询的控制器发布一些简单的数据。这个查询将返回JSON,我将继续我的快乐之路,然后对数据做我想做的事情。

I am having no problems with this when using built in jquery AJAX. Here is the working code:

在使用jquery AJAX构建时,我没有遇到任何问题。以下是工作代码:

$.post('/rx/dostuff', {fromDate: from_date, toDate: to_date}, function(data){                
            var dataObject = JSON.parse(data);
            if(data !== "[]"){
                $("#display_area").empty();
                $("#display_area").append("<div> " + ...

            blah blah blah

So this works great, I POST the from_date and the to_date, and I can see in the Network Params tab that it is being sent as

这很有用,我发布了from_date和to_date,我可以在Network Params选项卡中看到它被发送为

fromDate: 02/01/2016
toDate:   02/24/2016

which my PHP controller picks up, I do some statement binding to keep it safe, and boom, my results are returned to me JSON encoded.

我的PHP控制器接收到,我做了一些语句绑定来保证它的安全性,然后,我的结果返回给我JSON编码。

When I try to do this using the DataTables library, my params end up being sent like this:

当我尝试使用DataTables库进行此操作时,我的params最终被发送为如下所示:

fromDate=02%2F01%2F2016&toDate=02%2F24%2F2016

PHP raises hell, and throws a warning that I am missing argument 1 for my controller.

PHP会引起麻烦,并发出警告说,我正在为我的控制器丢失参数1。

My question is, why is DataTables sending the params like this? Have I malformed the way that it is meant to be sent?

我的问题是,为什么DataTables像这样发送params ?我没有按预定的方式发送吗?

Here is my DataTables AJAX code:

以下是我的DataTables AJAX代码:

$('#dataTable').DataTable({
        ajax: {
            url: "/rx/dostuff",
            type: "POST",
            contentType: "application/json",
            data: {
                "fromDate": from_date,
                "toDate": to_date
            }
        },
        columns: [
            { data: 'col'},
            { data: 'col'},
            { data: 'col'},
            { data: 'col'}
        ]
    });

Any help is much appreciated!

非常感谢您的帮助!

1 个解决方案

#1


1  

Remove contentType: "application/json".

删除contentType:“application / json”。

This parameter defines the format when sending data to the server. Default is "application/x-www-form-urlencoded; charset=UTF-8" which should be working for you according to what you've used before with $.post().

此参数定义向服务器发送数据时的格式。默认的是“应用程序/ x-www-form-urlencoded;charset=UTF-8”,根据您以前使用的$.post(),应该可以使用它。

See $.ajax() for more information.

有关更多信息,请参见$.ajax()。

#1


1  

Remove contentType: "application/json".

删除contentType:“application / json”。

This parameter defines the format when sending data to the server. Default is "application/x-www-form-urlencoded; charset=UTF-8" which should be working for you according to what you've used before with $.post().

此参数定义向服务器发送数据时的格式。默认的是“应用程序/ x-www-form-urlencoded;charset=UTF-8”,根据您以前使用的$.post(),应该可以使用它。

See $.ajax() for more information.

有关更多信息,请参见$.ajax()。