在服务器端分页上防止来自jquery数据表的多个ajax调用 [英] Prevent multiple ajax calls from jquery datatable on server side pagination

查看:91
本文介绍了在服务器端分页上防止来自jquery数据表的多个ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个jquery dataTable,它使用ajax向MVC控制器发送请求并显示数据. 它可以在客户端处理中很好地工作,但是响应时间太慢,因为它会检索所有记录.

I have a jquery dataTable which sends a request to MVC controller using ajax and displays the data. It works fine with client side processing but response time is too slow because it retrieves all the records.

为使其更快,需要在服务器端进行分页. 服务器端分页的问题在于,在jquery数据表中将服务器端分页设置为true会发出多个ajax请求.

To make it faster server side pagination is required. The issue with server side pagination is that setting server side pagination to true in jquery datatable it makes multiple ajax requests.

我已经对其进行了大量调试,但无法弄清楚导致多个ajax请求的问题.我已经实现了Google提供的许多不同方法,但是没有用.

I have been debugging it a lot but cant figure the problem which causes multiple ajax requests. I have implemented many different ways available on google but nothing worked.

如何使用服务器端分页使其工作? 如何防止多次ajax调用? (表在document.ready()下初始化)

How to make it work using server side pagination? How can I prevent multiple ajax calls? ( table is initialized under document.ready() )

document.ready(function() {

            $("#myTable").DataTable({
            "processing": true, // for show progress bar
            "serverSide": true, // for process server side
            "orderMulti": false, // for disable multiple column at once
            "ajax": {
                "url": "/home/LoadData",
                "type": "Get",
                "datatype": "json"
                "data":{ date:'date'},// parameter on controller to filter records 
            },
            "columns": [
                    { "data": "ContactName", "name": "ContactName", "autoWidth": true },
                    { "data": "CompanyName", "name": "CompanyName", "autoWidth": true },
                    { "data": "Phone", "name": "Phone", "autoWidth": true },
                    { "data": "Country", "name": "Country", "autoWidth": true },
                    { "data": "City", "name": "City", "autoWidth": true },
                    { "data": "PostalCode", "name": "PostalCode", "autoWidth": true }
            ]
        });

第一次调用返回的数据正是我想要的,但是多次请求使情况变得更糟.

The data returned on first call is exactly what I want but multiple requests make it worse.

推荐答案

即使我已经为ajax成功方法定义了列定义,我仍在将数据添加到表中. 删除成功方法可以解决问题.

I was adding the data to table on ajax success method even though I had defined the column definitions for it. Removing the success method solved the problem.

这篇关于在服务器端分页上防止来自jquery数据表的多个ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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