function get_settings(ans){ var request = getXmlHttp(); // глобальная переменная var answer; request.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { return request.responseText; /*alert(request.responseText)*/ } } request.open("POST", "ajax.php", true); request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); request.send("act=get_set&ans="+ans); } 

how to return ' request.responseText ?

alert (get_settings('info')) gives undefined

and if you remove the quotes (/ ** /) gives what you need.

    1 answer 1

    Problems with the mat-part ... Just described something similar. In general, in order for your code to work:

     function get_settings(ans){ var request = getXmlHttp(); // глобальная переменная var answer; request.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { onRequestComlete(request.responseText); } } request.open("POST", "ajax.php", true); request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); request.send("act=get_set&ans="+ans); } function onRequestComplete(data) { // обработчик ответа сервера alert(data); // сдесь пишите код который должен отработать после получения // данных от сервера } 

    If you need something, the script would "fall asleep" until there is no response from the server, use a synchronous request, or something from the one I described here

    Now I will explain why you wrote something like this: onreadystatechange this event and your code

      if (this.readyState == 4 && this.status == 200) { return request.responseText; } 

    It is executed when this event occurs, moreover, also under additional conditions, in general, the model of asynchronous ajax request is event oriented, i.e. you cannot write linear code, you must describe the code in chains of events, or, as I wrote, use synchronous ajax (it was invented in order to write linear code)


    I wrote - for this, use a synchronous (blocking) ajax request, at the time of its execution, the page seems to freeze (and if at this moment the IE7 connection is not answered), but then you can write what you want.

    To make using synchronous ajax request:

      function get_settings(ans){ var request = getXmlHttp(); // глобальная переменная var answer; windows.status("ожидание ответа сервера..."); request.open("POST", "ajax.php", false); request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); request.send("act=get_set&ans="+ans); return request.responseText; } var settings = get_settings(""); alert(settings) 
    • It means that the get_settings function returns request.responseText is not possible to do? - Vitaly Zaslavsky
    • possible, but not necessary. + You have already mentioned 2 times how this can be done. - Alex Kapustin
    • Thank. I just do not know this language, therefore I am stupidly hard - Vitaly Zaslavsky
    • one
      Many people do not know him, and even more people think that they know him: D Yes, js is not like all xD - Zowie