How to get data from the server using the jQuery DataTables plug-in

Source: Internet
Author: User

The value of sAjaxSource is url. Table sends an ajax request to obtain data from the server. The data returned by the server is a JSON string that can be converted into a JSON object. This string must be in strict JSON format. Otherwise, an error occurs. The key of the data object should be "aaData", for example, Js Code: {"aaData": {"columnA": "valueA", "columnB ": "valueB ",...}} set the bServerSide parameter to true, indicating that the server is used to process data. When sorting is performed, the data is directly queried in the background and displayed directly, without sorting at the front end. The fnServerData parameter uses a custom function instead of the default function used by the DataTables plug-in to query data from the server. The default function is as follows: Js Code:/*** @ param {string} sSource HTTP source to obtain the data from (sAjaxSource) * @ param {array} aoData A key/value pair object containing the data to send * to the server * @ param {function} fnCallback to be called on completion of the data get * process that will draw the data on the page. * @ param {object} oSettings DataTables settings object */"fnServerData": function (sUrl, aoD Ata, fnCallback, oSettings) {oSettings. jqXHR = $. ajax ({"url": sUrl, "data": aoData, "success": function (json) {if (json. sError) {oSettings. oApi. _ fnLog (oSettings, 0, json. sError) ;}$ (oSettings. oInstance ). trigger ('xhr', [oSettings, json]); fnCallback (json);}, "dataType": "json", "cache": false, "type": oSettings. sServerMethod, "error": function (xhr, error, thrown) {if (error = "pa Rsererror ") {oSettings. oApi. _ fnLog (oSettings, 0, "DataTables warning: JSON data from" + "server cocould not be parsed. this is caused by a JSON formatting error. ") ;}}) ;}, you can use this parameter to customize ajax requests, or process the obtained data. For example, if the data object returned from the table on the server does not use "aaData" as the data key, we can add "aaData" key to the data in our defined callback function. The fnServerParams parameter is used to send additional data to the server. Example: Js Code: $ ('# example '). dataTable ({"bProcessing": true, "bServerSide": true, "sAjaxSource": "scripts/server_processing.php", "fnServerParams": function (aoData) {aoData. push ({"name": "more_data", "value": "my_value "});}});

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.