jquery - How can I store my ajax data in a global variable? -


$(document).ready (function() {      var shuffle = function(a) {     for(var j, x, = a.length; i; j = parseint(math.random() * i), x = a[--i],                    a[i] = a[j], a[j] = x);     return a; }; 

i want local variable 'fullname' have ajax data , want use global variable 'fullname'

var fullname; var randomshuff = shuffle(fullname);  var = 0  $(".runrandompart").on('click', function() {      if(i == randomshuff.length)            = 0;      return $('.main').html(randomshuff[i++]); }); 

here's ajax data i'm trying store in global variable. maybe it's scope issue.

$.ajax({     url: "/api/students",     datatype: 'json',     success: function(results){          var arr = results;          (d=0; d<arr.length; d++) {               fullname = arr[d].last_name + ", " + arr[d].first_name;          }     } }); 

i tried putting functionality in ajax function doesn't work either.

try using window.fullname instead.


Comments