asprin
asprin

Reputation: 9823

Access javascript variable declared/defined/initialized in ajax page

I make an ajax call with:

index.php

$.ajax({
  type:'post',
  url:'abc.php',
  success:function(returned_data)
  {
     // returned_data  contains HTML + javascript code
     // I'd like to access the javascript variable here
     // refer to abc.php's code to see what variable
     // somehow access "new_variable" here
  }
})

abc.php

<table id="first_table">
 <tr>
   .
   .
   .
  <!-- some un-related td content -->
 </tr>
</table>
<script>
  new_variable = $('#first_table').dataTable();
  // now I want new_variable to be available in index.php page; i.e. inside the success
  // function of the ajax method
</script>

Can this be achieved? Or any alternative to achieve this?

Upvotes: 0

Views: 46

Answers (2)

asprin
asprin

Reputation: 9823

Surprisingly, the problem seemed to vanish by itself. I don't know what minor change I did or didn't, but simply using:

$.ajax({
  type:'post',
  url:'abc.php',
  success:function(returned_data)
  {
     console.log(new_variable);
  }
})

printed the result appropriately...which didn't happen earlier before asking this question. What's more is since it is global variable, I could access it outside this ajax function too xD

Upvotes: 0

Adrian Forsius
Adrian Forsius

Reputation: 1438

You could do something like this instead:

   $.ajax({
        type:'post',
        url:'abc.php',
        success:function(data)
        {
            var new_variable = $(data).find('#first_table').dataTable();
        }
    });

Upvotes: 1

Related Questions