cross domain ajax works but there's no data displayed in my table : (

cross domain ajax works but there's no data displayed in my table : (

hsajjadhsajjad Posts: 28Questions: 0Answers: 0
edited February 2014 in DataTables 1.9
https://datatables.net/release-datatables/examples/server_side/jsonp.html

The above setting works (below) - confirmed on the firebug, the JSON data is retrieved, but my issue is it's not shown on the table!!

Any clues why??

[code]
$(document).ready(function() {
$('#table_id').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "http://localhost:9001/api/gui/messages",
"fnServerData": function( sUrl, aoData, fnCallback, oSettings ) {
oSettings.jqXHR = $.ajax( {
"url": sUrl,
"data": aoData,
"success": fnCallback,
"dataType": "jsonp",
"cache": false
} );
},
"bDeferRender": true,
"aoColumns": [
{ "mData": "busId", "bSortable": false },
{ "mData": "sourceSystem", "bSortable": false },
{ "mData": "receiveTime", "bSortable": false },
...
],
"sDom": "rtp",
"sPaginationType": "full_numbers",
"iDisplayLength": 20
} );
} );

[/code]


Just to verify things, if source data locally i.e. replace:
"sAjaxSource": "http://localhost:9001/api/gui/messages",
"fnServerData" ...

with something like:
"sAjaxSource": "resources/data.txt",

then all works!

I'm using DataTables 1.9.4

Thanks!

Replies

  • allanallan Posts: 61,880Questions: 1Answers: 10,139 Site admin
    We would need a link to the page to be able to figure out why it isn't working. As you can see from my example, it can and should work :-)

    Allan
  • hsajjadhsajjad Posts: 28Questions: 0Answers: 0
    Hi Allan

    I just noticed, your example does not load any data when run locally on my machine - using Firefox 4 or IE 8. Both browsers display 'Loading data from server' and remain at it!

    Example:
    file:///C:/DataTables-1.9.4/examples/server_side/jsonp.html

    Is this supposed to work..?

    Thanks
  • tangerinetangerine Posts: 3,350Questions: 37Answers: 394
    edited February 2014
    [quote] file:///C:/DataTables-1.9.4/examples/server_side/jsonp.html[/quote]
    That's not going to do much unless you replace "file:" with "localhost:" and fix the path appropriately..
  • hsajjadhsajjad Posts: 28Questions: 0Answers: 0
    Tangerine, the path used there is only to point to the local example of JSONP that ships with DT. Running that doesn't work / display data on my system, however, not sure if this is because of no built-in PHP support on my browser/system or else. I've tried Chrome and IE 11, same fate!
  • allanallan Posts: 61,880Questions: 1Answers: 10,139 Site admin
    > file://

    As tangerine says, this won't work. Browsers have built in security to stop exactly this. It would ba a _massive_ security hole if any site could just randomly load files from your hard disk!

    Thee is no way around that. You need a web server.

    Allan
  • hsajjadhsajjad Posts: 28Questions: 0Answers: 0
    Not sure if this is due to cross domain / jsonp related or just server side rendering issue.

    Moved the server part from remote to localhost (within same webapp) as:

    [code]
    "sAjaxSource": "query.do?id=777",
    [/code]

    and removed 'fnServerData' part. Still no fun, I can see the JSON being fetched successfully (as before) but no data display on the dtable.

    [quote]
    {
    "aaData":[{"busId":"777","domainId":1,"sourceSystem":"TEST-GW","receiveTime":1386931764000,"validationStatus":"V","messageId":"456-yy","messageType":"tradeCreate","routingStatus":"R","businessIds":[{"scheme":"http://test-gw.jpm.com","value":"123456","version":""}],"recipients":[{"systemId":"NAPOLI-TS","sentTime":1386931764000,"deliveryStatus":"F","deliveryFailureReason":null},{"systemId":"TEST-GW","sentTime":1386931764000,"deliveryStatus":"D","deliveryFailureReason":null}]}],
    "iTotalRecords":1,
    "iTotalDisplayRecords":1,
    "sEcho":0
    }
    [/quote]

    All is hunky dori if you point sAjaxSource to a text file with this JSON data.

    Any help is highly appreciated.
  • hsajjadhsajjad Posts: 28Questions: 0Answers: 0
    Allan, re file:// - ok I see your point. This was an additional verification attempt using your samples.

    For my app, I do deploy it on Tomcat though using localhost. And I can see on Firebug the ajax is getting executed and fetching data as expected, it's just not displayed, as per my last comment.
  • hsajjadhsajjad Posts: 28Questions: 0Answers: 0
    This is fixed - was due to the internal pagination / state params used by DT (sEcho etc) that weren't getting relayed back n forth by my server side component. Fixing that fixes the rendering problem!
  • allanallan Posts: 61,880Questions: 1Answers: 10,139 Site admin
    Good to hear :-)
This discussion has been closed.