Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts

    Get the responceText to return from AJAX

    The alert from inside the function returns the response text but the call doesn't.

    I imagine that's because the function returns before the ajax call has finished?

    How can I get it to return the responseText, or is it impossible to do like this?

    Code:
    function ajaxPOST(url, data) {
                    var r = new XMLHttpRequest();
                    r.open('POST', url, true);
                    r.onreadystatechange = function() {
                        if (r.readyState === 4)
                        {
                            alert('R: '+r.responseText);
                            return(r.responseText);
                        }
                    };
                    r.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                    r.send(data);
                }
                
                alert(ajaxPOST('ajax.php', 'arg=test'));

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Yes, as you say, the function has already completed, and the value returned by your anonymous function just disappears.

    It is possible to use a global variable and set it to the responseText, but this is of no use because you have no (reasonable) way to confirm that this data is now available. (I only mention this for the sake of completeness.)

    All the processing of the responseText should occur within your onreadystatechange event. That is, where you are currently just displaying an alert. You could, if you like, pass the responseText to another helper function.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,246
    Thanks
    12
    Thanked 340 Times in 336 Posts
    Quote Originally Posted by tomharto View Post
    I imagine that's because the function returns before the ajax call has finished?
    that’s the nature of an asynchronous function. and the ajaxPOST() function itself does not even have a return statement ...

    Quote Originally Posted by tomharto View Post
    How can I get it to return the responseText, or is it impossible to do like this?
    only by making it a synchronous request. though it would be debateable why you want the AJAX function have a return value.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,296
    Thanks
    10
    Thanked 583 Times in 564 Posts
    Code:
    r.open('POST', url, false);
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •