Skip to content Skip to sidebar Skip to footer

Access A Variable From Inside Ajax Call Success

I have the following in my js file: var highestNumb; $.ajax({ url: 'https://api.m.hostelworld.com/1.5/properties/'+propID+'/?update-cache=true', dataType: 'js

Solution 1:

You should in your success function call another function, eg. setVars(highestNumb) and put your code that sets enH3, deH3 etc. variables in that function.

success: function(json) { 
   //your code
   setVars(numb);
}

function setVars(highestNumb)
{
    enH3 = 'Fellow travellers have rated this property ' + highestNumb + ' out of 100.';
    //....
}

Solution 2:

Please put async:false

var highestNumb; $.ajax({ url: 'https://api.m.hostelworld.com/1.5/properties/'+propID+'/?update-cache=true', dataType: 'json', async:false, headers: { "Accept-Language": lang }, success: function(json) { var numb = 0; for (var key in json.rating) { numb = Math.max(numb, json.rating[key]); } highestNumb = numb; return highestNumb;

      }, cache: false

});


Post a Comment for "Access A Variable From Inside Ajax Call Success"