sAjaxSource not working for Java servlet

sAjaxSource not working for Java servlet

cpettifercpettifer Posts: 12Questions: 0Answers: 0
edited October 2012 in General
I've got a Java servlet that is returning valid JSON data with the content type set to "application/json" (as suggested elsewhere. The JSON data looks like this (saved by going to my servlet's url):

{"sEcho":2,"iTotalRecords":2,"iTotalDisplayRecords":2,"aaData":[{"name":"Name1","value":"Value1"},{"name":"Name2","value":"Value2"}]}

My datatables declaration looks like this:

$(function(){
$('#DataTable').dataTable( {
"bProcessing": true,
"sAjaxSource": "http://localhost:8080/test.admin",
"aoColumns": [
{ "mData": "name" },
{ "mData": "value" }
]
} );
});

I can see that the data being returned is correct (breakpoint in my java code), but the datatable on my page just has "Loading..." in it and no data.

If I save the JSON data from the URL to a text file, say ajax.txt, and then set this to be the sAjaxSource, i.e. "sAjaxSource": "ajax.txt", then the page works as expected and my table is populated with two rows.

Anyone got any clues as to what might be the problem? Is it something in my java or something else?

Replies

  • allanallan Posts: 63,389Questions: 1Answers: 10,449 Site admin
    Unless your page is being loaded from localhost:8080, then you are running into cross site scripting problems (does Firebug / Inspector not warn about this?). You'd need to use JSONP if you are loading data from a different domain / port.

    Allan
  • cpettifercpettifer Posts: 12Questions: 0Answers: 0
    edited October 2012
    Ah OK, thanks. I had a mocked up html page I loaded in my browser from a file, but was calling a servlet running properly! Just moved the code into a jsp on the same server and referred to the servlet with just /test.admin and all works as expected now!

    Doh.
  • cyberwarriorcyberwarrior Posts: 5Questions: 0Answers: 0
    Hello,

    i have a question that seems to be near this subject.
    I try to use a datatable with a java server. So i read the exemple on this page: http://datatables.net/development/server-side/jsp. I was surprise to see that the code is in a JSP and not in a servlet. By reading this post, i guess it is cross site scripting problems that justify this ?

    The Real question is related directly to this subject: do we need to use a special jsp for the datatable server code and to put the view of datatable in another jsp ? i m quite sure it is not possible to put all both on the same jsp. In both case, what is the link for the "sAjaxSource" datatable parameter.
    Do we finally need a servlet to make this run with the special jsp

    Thanks
  • cyberwarriorcyberwarrior Posts: 5Questions: 0Answers: 0
    no one can light me on the "sAjaxSource" value for calling a jsp ?
    This point let me doubtful because in my logical, we call always a servlet declared in the file web.xml.
    Is it a special jsp or the same jsp where the widget datatables is ?


    ++
  • cpettifercpettifer Posts: 12Questions: 0Answers: 0
    If I were you I'd write a servlet to serve the JSON data to your datatable. The url just needs to be a relative url / hosted on the same server as the calling page. Your datatable could be in a plain .html file calling a servlet hosted on the same box.
  • azitbadazitbad Posts: 1Questions: 0Answers: 0
    I am new to datatables. I got a similar problem where I am not able to call a servlet. My javascript code is as below:


    $(document).ready(function() {

    $('#dynamic').html( '');

    $('#example').dataTable({
    "bProcessing" : true,
    "bServerSide": true,
    "sPaginationType": "full_numbers",
    "sAjaxSource" : "http://localhost/TMC/DataGridJson",
    "sServerMethod" : "POST"
    });
    });


    My JSP code is below - just to provide a section for dynamic table :



    The call to servlet is not successful. I inserted simple system out print commands to verify the entry into the servlet for both doGet and doPost methods. I gave right URL of the servlet as it prints the system out when URL is submitted in browser.

    Can anyone please help me with solving the problem? Is there any syntax issue?
  • MarzioForteMarzioForte Posts: 1Questions: 0Answers: 0
    I too am having a problem with a DataTable component not calling a servlet. I tested the datatable code by isolating and running it in a different JSP/Servlet, and it worked just fine. This problem seems to be specific to the JSP I am trying to use. I am using several other components like chosen, themeswitcher, and a few others.... Are there known conflicts with between the datatable widget and other jquery components? Any ideas would be welcome. I do not now how to proceed on this one. Thank you in advance.
This discussion has been closed.