Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 22nd, 2012  |  syntax: None  |  size: 1.70 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. jquery async and JSON data
  2. data1=[1,2,3,4]
  3.        
  4. $(document).ready(function(){
  5.  
  6. var myArray=[];
  7. myArray=getValues();
  8. alert(myArray);
  9.         function getValues(){
  10.         var result=null;
  11.              $.ajax({
  12.                 url: 'data1.html',
  13.                 type: 'get',
  14.                 dataType: 'json',
  15.                 cache: false,
  16.                 success: function(data) {result = data;},
  17.                 async:true,
  18.                 });
  19.             return result;
  20.         };
  21. })
  22.        
  23. $(document).ready(function(){
  24.  
  25. var myArray=[];
  26. myArray=getValues();
  27. alert(myArray);
  28.         function getValues(){
  29.         var result=null;
  30.              $.ajax({
  31.                 url: 'data1.html',
  32.                 type: 'get',
  33.                 dataType: 'json',
  34.                 cache: false,
  35.                 success: function(data) {result = data;},
  36.                 async:false,
  37.                 });
  38.             return result;
  39.         };
  40.  })
  41.        
  42. function postProcessing(data) {
  43.    var myArray = data;
  44.    alert(myArray);
  45.  }
  46.  
  47.  
  48. getValues();
  49.  
  50.     function getValues(){
  51.          $.ajax({
  52.             url: 'data1.html',
  53.             type: 'get',
  54.             dataType: 'json',
  55.             cache: false,
  56.             success: function(data) {postProcessing(data);},
  57.             async:true,
  58.             });
  59.     };
  60.        
  61. <script src="jquery.js"></script>
  62. <script>
  63. $(document).ready(function(){
  64.  
  65.     /*don't do your stuff here*/
  66.         /*do inside success*/
  67.  
  68.     function getValues(){
  69.         var result=null;
  70.         $.ajax({
  71.             url: 'phpinfo.php',
  72.             type: 'get',
  73.             dataType: 'json',
  74.             cache: false,
  75.             success: function(data) { if(data != null){ alert(data); } },
  76.         });
  77.         return result;
  78.     };
  79.  
  80. })
  81. </script>