JSONP data source clarification
JSONP data source clarification
I am trying to load data from a remote source and I followe exactly what is mentioned on the link
https://datatables.net/examples/server_side/jsonp.html
But I cannot get it to work. It says invalid JSON. WHat I noticed is that on the link above under the ajax tab the sample data is shown as
{
"draw": 1,
"recordsTotal": 57,
"recordsFiltered": 57,
"data": [
[
"Airi",
"Satou",
"Accountant",
"Tokyo",
"28th Nov 08",
"$162,700"
],
But this is not valid JSONP data it is just regular JSON. So If I have to upload this same data on the remote server what should it look like for JSONP? some examples show that it should be enclosed in a callback function like
callback_1234({......});
but if I do that then what change do I need to make on the javascript side of
$(document).ready(function() {
$('#example').DataTable( {
"processing": true,
"serverSide": true,
"ajax": {
"url": "scripts/jsonp.php",
"dataType": "jsonp"
}
} );
} );
Answers
Are you using the serverside script from the example?
The data is being provided to me by backend developers. But before I ask them to make any changes I need to know if I need to make any changes to the javascript on the fron-end.
Your code above refers to:
Is that the serverside script from the example?
The actual data is this
http://dev.cehtp.org/api/water/search/systems
I know it is not JSONp and simply JSON but before I ask them to wrap it in a callback function I need to know what changes I need to make on the frontend javascript too
I already asked you the same question twice. Let's try one more time in different words.
What is "url": "scripts/jsonp.php" doing in your code?
It is not my code. it is the sample code from the datatables.net example
https://datatables.net/examples/server_side/jsonp.html
My code is this exact same copy of that code with just a different url:
Tangerine is asking if your jsonp.php script has the callback code like the example you linked. Look at the Server-side script tab. For example:
I looked at your JSON response and while the format is valid the data structure for Datatables server side processing is not. Here is your JSON data:
This page explains the format expected. Instead of
"total": 10,
the JSON data should contain something like this:The standard for Datatables is to return the data in a
data
object where you are usingrows
. If you want to userows
then you need to useajax.dataSrc
to change from the default ofdata
.If you go to the example you referenced, open the browser's developer tools and go to network you can see that the callback is in the URL for jsonp.php with the JSON response.
Kevin
I can't add much to what Kevin and @tangerine have already said, as they are excellent answers, but I should point out that in the JSONP example that you linked to, you rightly point out that the Ajax tab shows plain JSON rather than JSONP. That's because it uses the DataTables API to get the data returned from the server, which is just the plain JSON data, rather than with the callback function.
If you use the "Network" panel in your browser's extension you would be able to see it actually looks like:
Sorry for the confusion.
Allan