JSONP is one of several methods to allow the use of JSON data from any server
(browsers have XSS protection rules which will block standard Ajax requests to remote source for security reasons). Using JSONP allows DataTables to load
server-side sourced data from any domain and is quite simply done with the dataType
option of the ajax
initialisation option.
When given as an object, the ajax
option maps directly onto the jQuery ajax options (i.e. any option that can be used in jQuery's Ajax function can
also be used in DataTable's ajax
option).
The example below shows ajax
being
used with the dataType
option set to retrieve JSONP data for server-side processing in DataTables.
The Javascript shown below is used to initialise the table shown in this example:
1 2 3 4 5 6 7 8 9 10 | $(document).ready( function () {
$( '#example' ).DataTable( {
"processing" : true ,
"serverSide" : true ,
"ajax" : {
"url" : "scripts/jsonp.php" ,
"dataType" : "jsonp"
}
} );
} );
|
In addition to the above code, the following Javascript library files are loaded for use in this example:
The HTML shown below is the raw HTML table element, before it has been enhanced by DataTables:
<table id="example" class="display" style="width:100%">
<thead>
<tr>
<th>First name</th>
<th>Last name</th>
<th>Position</th>
<th>Office</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tfoot>
<tr>
<th>First name</th>
<th>Last name</th>
<th>Position</th>
<th>Office</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
</table><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="js plain">$(document).ready(</code><code class="js keyword">function</code><code class="js plain">() {</code></div><div class="line number2 index1 alt1"><code class="js spaces"> </code><code class="js plain">$(</code><code class="js string">'#example'</code><code class="js plain">).DataTable( {</code></div><div class="line number3 index2 alt2"><code class="js spaces"> </code><code class="js string">"processing"</code><code class="js plain">: </code><code class="js keyword">true</code><code class="js plain">,</code></div><div class="line number4 index3 alt1"><code class="js spaces"> </code><code class="js string">"serverSide"</code><code class="js plain">: </code><code class="js keyword">true</code><code class="js plain">,</code></div><div class="line number5 index4 alt2"><code class="js spaces"> </code><code class="js string">"ajax"</code><code class="js plain">: {</code></div><div class="line number6 index5 alt1"><code class="js spaces"> </code><code class="js string">"url"</code><code class="js plain">: </code><code class="js string">"scripts/jsonp.php"</code><code class="js plain">,</code></div><div class="line number7 index6 alt2"><code class="js spaces"> </code><code class="js string">"dataType"</code><code class="js plain">: </code><code class="js string">"jsonp"</code></div><div class="line number8 index7 alt1"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number9 index8 alt2"><code class="js spaces"> </code><code class="js plain">} );</code></div><div class="line number10 index9 alt1"><code class="js plain">} );</code></div></div></td></tr></tbody></table><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2" style="display: none;">1</div></td><td class="code"><div class="container" style="display: none;"><div class="line number1 index0 alt2" style="display: none;"> </div></div></td></tr></tbody></table>
This example uses a little bit of additional CSS beyond what is loaded from the library files (below), in order to correctly display the table. The
additional CSS used is shown below:
The following CSS library files are loaded for use in this example to provide the styling of the table:
This table loads data by Ajax. The latest data that has been loaded is shown below. This data will update automatically as any additional data is
loaded.