0

so what im doing is im loading some data with a get resquest and i get this variable on httpRequest.responseText and im using datatables to have some fancy table but the problem is i can not load my js var on ajax : field ..

 <script type="text/javascript">
        var httpRequest = null;

        function SendRequest () {
            if (!httpRequest) {
                httpRequest = CreateHTTPRequestObject ();   // defined in ajax.js
            }
            if (httpRequest) {          
                    // The requested file must be in the same domain that the page is served from.
                var url = '/resources/ud_auto_executions';
                httpRequest.open ("GET", url, true);    // async
                httpRequest.onreadystatechange = OnStateChange;
                httpRequest.send (null);
            }
        }

        function OnStateChange() {
            if (httpRequest.readyState==4) {
                if (IsRequestSuccessful (httpRequest)) {    // defined in ajax.js
                alert ("hello im coming");

                }
                else {
                    alert ("Operation failed.");
                }
            }
       }
</script>

i have trouble on the ajax :

  <script>
        $(document).ready(function() {
        $('#example').DataTable( {
            "AutoWidth": true,
            "ajax": httpRequest.responseText
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Patrick Evans Jun 20 '16 at 14:30
  • This is not the same problem – Med Oulaidi Jun 20 '16 at 14:34
  • @MedOulaidi You'll have to explain why you think the linked answer does not apply here. You're trying to use `httpRequest.responseText` in document ready *before* it has returned from the asynchronous call - ie you need help on how to return the response an asynchronous call - ie the linked question. Maybe you just need to look up "asynchronous"? Hard to tell. – freedomn-m Jun 20 '16 at 14:39
  • @freedomn-m i just dont know how to apply it on my case ... – Med Oulaidi Jun 20 '16 at 14:52
  • Simply move your `$().DataTable` call into the ajax response (OnStateChange) callback where `httpRequest.responseText` will then be in scope. `if (IsRequestSuccessful (httpRequest)) { ` becomes `if (IsRequestSuccessful (httpRequest)) { $("#example").DataTable({...` – freedomn-m Jun 20 '16 at 15:26

1 Answers1

0

You are over complicating things. Try: https://datatables.net/examples/index

Andrei Zhytkevich
  • 8,039
  • 2
  • 31
  • 45