0

I have a function which attempts to capture a return value from a calling function in the following manner:

var select = xhrRetrieve(projID);

Here is an example of the xhrRetrieve function:

function xhrRetrieve(projID) {
    var xhr = new XMLHttpRequest();

    xhr.onreadystatechange = function() {
        if(xhr.readyState == 4) {
            if(xhr.status == 200) {
                var obj = $.parseJSON(xhr.responseText);

                return obj.select.toString();
            }
        }
    }

    var url = "ajax.cgi";
    var data = "action=retrieve-opp&proj-id=" + projID;

    xhr.open("POST",url);
    xhr.setRequestHeader("Content-Type","application/x-www-urlencoded");
    xhr.send(data);

}

I am using jQuery in conjunction with straight JavaScript. Whenever I attempt to get the value of obj.select using:

var select = xhrRetrieve(projID);

Select always comes back undefined.

What am I doing wrong?

4 Answers 4

1
  1. The function doesn't return anything
  2. The moment you call your function, the (not currently present) return value is being assigned to select. At the same moment, your ajax request is being fired, which takes time to complete; the callback function will not be called until the ajax request has completed (and succeeded).

This should work:

function doStuffWithTheAjaxResponse(select) {
   // do stuff
}

function xhrRetrieve(projID) {
    var xhr = new XMLHttpRequest();

    xhr.onreadystatechange = function() {
        if(xhr.readyState == 4) {
            if(xhr.status == 200) {
                var obj = $.parseJSON(xhr.responseText);

                doStuffWithTheAjaxResponse(obj.select.toString());
            }
        }
    }

    var url = "ajax.cgi";
    var data = "action=retrieve-opp&proj-id=" + projID;

    xhr.open("POST",url);
    xhr.setRequestHeader("Content-Type","application/x-www-urlencoded");
    xhr.send(data);

}
Sign up to request clarification or add additional context in comments.

Comments

1

Since the request is asynchronous the function will return before your code in onreadestatechange fires. You can switch to synchronous and get the value before the function returns:

function xhrRetrieve(projID) {
    var returnVal;
    var xhr = new XMLHttpRequest();

    var url = "ajax.cgi";
    var data = "action=retrieve-opp&proj-id=" + projID;

    //3rd param is false to switch to synchronous
    xhr.open("POST",url, false);
    xhr.setRequestHeader("Content-Type","application/x-www-urlencoded");
    xhr.send(data);
    if(xhr.readyState == 4) {
        if(xhr.status == 200) {
            var obj = $.parseJSON(xhr.responseText);
            return obj.select.toString();
        }
    }
}

Comments

0

The function xhrRetrieve doesn't have a return value. What do you expect to happen?

2 Comments

does not the return obj.select.toString(); inside of xhr.onreadystatechange achieve this?
No, that will only happen when that XHR event gets fired. If you want a blocking call, you will need to either set async=false when you call xhr.send; or better yet use an asynchronous solution like the snippet Bjorn posted.
0

You have two functions there. The inner function returns a value, but not the outer one. The inner function is an event handler so the return value doesn't go anywhere. Your XMLHttpRequest is asynchronous, so you won't get a return value right away. See this post for a more detailed explanation: parameter "true" in xmlHttpRequest .open() method

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.