There is a jQuery AYAX request that loads the name of the template when the page loads.

var tmp_name = ''; $.ajax({ url: shopScript, type: 'POST', dataType: 'json', data: { action: 'get_tmp_name' }, success: function(data) { tmp_name = data.data.tmp; console.log(tmp_name);//Результат: russian console.log(window.tmp_name);//Результат: russian } }); console.log(tmp_name);// Результат: ''; console.log(window.tmp_name);// Результат: ''; 

And so the question is why in success does not overwrite the value of a global variable?

  • According to your comments in the code it turns out that it overwrites. - Vladimir Gamalyan
  • No, after calling the AJAX zaprosa - the value of the variable TO AYAKS was an empty string, and after the AYAX there should be a response line. Solved the issue, added async to AYAX: false; - Flayter
  • There are several options for solving the asynchrony task ru.stackoverflow.com/questions/524835/… - Serge Esmanovich

1 answer 1

Solved the issue by adding a request to Ajax: async: false