0

Why cant i work with ajax output in other code?

var str1 = "";
$.ajax({
    type: 'POST',
    url: '/main.php',
    success: function(data){
        data = minify_html(data);
        str1 = data;
        alert(str1); // return minified html code
    }
   });
alert(str1); // return nothing
Alex Tartan
  • 6,736
  • 10
  • 34
  • 45
  • You probably want to do a GET request: `type: 'GET'`, since you're not posting anything to `main.php` – JiFus Jul 11 '17 at 19:21
  • I don't understand what it is that you're asking – j08691 Jul 11 '17 at 19:21
  • POST/GET is irrelevant. @j08691 they're asking how to get `str1` - but it's nothing because that code runs before the ajax call has completed. There's a clue in the name 'asynchronous'... see duplicate. (this gets asked about 3-4 times an hour...) – freedomn-m Jul 11 '17 at 19:23
  • So it's popular problem and i was trying to solve the problem for 2-3 hours myself but i couldnt and asked the question. – Mer Denchik Jul 11 '17 at 19:53

0 Answers0