用新参数重新加载Ajax请求 [英] Reload Ajax request with new parameters

查看:180
本文介绍了用新参数重新加载Ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过AJAX请求从数据库中获取表数据.而且我需要在AJAX请求中更改data参数并刷新表格.

I obtain table data from database via AJAX request. And I need to change data parameter in AJAX request and refresh the table.

我正在用命令刷新表

$('#table1').DataTable().ajax.reload();

我有以下代码

$('#table1').DataTable({

    /* SERVER SIDE PROCESSING */
                "serverSide": true,
                "ajax":
                    {
                        "url": "Home/Search",
                        "type": "POST",

                        "data": {
                            'searchType': GetSearchType(),
                            'searchText': GetSearchText()
                            //'searchType': $.mynamespace.searchType
                            //'searchText': $.mynamespace.searchText
                            //'searchType': localStorage.getItem("searchType"),
                            //'searchText': localStorage.getItem("searchText"),
                        }
                    }
            });

但是在重新加载AJAX之后,将原始请求发送到服务器,而新的参数值将被忽略.我试图通过函数,全局变量和浏览器存储将数据传递给请求,但没有一种方法起作用.在互联网上,我找到了

But after AJAX reload, original request to the server is sent and new parameter values are ignored. I tried pass the data to the request via function, global variable and browser storage but none of the approach work. On the internet I find solution with

aoData.push() 

功能,但我不知道如何使用.

function but I don't know how to use it.

我的jQuery DataTables版本是1.10.7.

My version of jQuery DataTables is 1.10.7.

我还尝试使用以下代码破坏并重新创建表:

I also tried destroying and recreating the table with this code:

$('#table1').DataTable({
        "ajax":
            {
                "url": "Home/Search",
                "type": "POST",

                "data": {
                    'searchType': GetSearchType(),
                    'searchText': GetSearchText()
                }
            },
        "destroy" : true
    }).ajax.reload();

但是我收到错误消息:

DataTables警告:表ID = table1-Ajax错误( http://www.datatables .net/manual/tech-notes/7 )

参数字典包含非空类型'System.Int32'的参数'draw'的空条目

The parameters dictionary contains a null entry for parameter 'draw' of non-nullable type 'System.Int32'

推荐答案

您可以将function用作 ajax.data 选项,如下所示.

You can use function as a value for ajax.data option as shown below.

这样,您的代码将在每次客户端向服务器发出请求时运行,而不是像您的初始代码那样运行.

That way your code will be run every time the client makes request to the server and not once as with your initial code.

$('#table1').DataTable({
   "serverSide": true,
   "ajax": {
      "url": "Home/Search",
      "type": "POST",
      "data": function(d){
         d.searchType = GetSearchType();
         d.searchText = GetSearchText();
      }
   }
});

然后在需要重新加载表时使用$('#table1').DataTable().ajax.reload();如果不想重设当前页面,请使用$('#table1').DataTable().ajax.reload(null, false).有关更多信息,请参见 ajax.reload() .

Then use $('#table1').DataTable().ajax.reload() when you need to reload the table or $('#table1').DataTable().ajax.reload(null, false) if you don't want to reset the current page. See ajax.reload() for more information.

这篇关于用新参数重新加载Ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆