最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to call a callback method after HTTP request - Stack Overflow

programmeradmin0浏览0评论

My code is running inside a main function. One part of my code is to make an HTTP request with an parameter which was defined before in the function and than write the response in to a new variable and work with it later.

I would like to exclude these steps with HTTP Request outside of the main function, and just CALL the function and write the response in a variable.

Unfortunately I tried it, but it doesn't work.

Error: variable is undefined

My code:

function DoWork() {
    //some code

    var strResponseHttpRequest;
    strResponseHttpRequest = HttpRequest(strInput, function(strInput) {
    console.log(strInput);
    };

    //continue working with the variable 'strResponseHttpRequest'   
    //rest of my code
}


function HttpRequest(strInput, callBackMethod) {

    var objRequest = new XMLHttpRequest(); //New request object

    objRequest.onreadystatechange = function() {

    // Waits for correct readyState && status
    if (objRequest.readyState == 4 && objRequest.status == 200) callBackMethod(objRequest.responseText)
    }

    objRequest.open("get", "php/get-content.php?ID=" + strInput, true);
    objRequest.send();
}

I hope you can help me to find out, where the issue is. If there are some better way to do this, let me know. I would be appreciate it. Thank you.

My code is running inside a main function. One part of my code is to make an HTTP request with an parameter which was defined before in the function and than write the response in to a new variable and work with it later.

I would like to exclude these steps with HTTP Request outside of the main function, and just CALL the function and write the response in a variable.

Unfortunately I tried it, but it doesn't work.

Error: variable is undefined

My code:

function DoWork() {
    //some code

    var strResponseHttpRequest;
    strResponseHttpRequest = HttpRequest(strInput, function(strInput) {
    console.log(strInput);
    };

    //continue working with the variable 'strResponseHttpRequest'   
    //rest of my code
}


function HttpRequest(strInput, callBackMethod) {

    var objRequest = new XMLHttpRequest(); //New request object

    objRequest.onreadystatechange = function() {

    // Waits for correct readyState && status
    if (objRequest.readyState == 4 && objRequest.status == 200) callBackMethod(objRequest.responseText)
    }

    objRequest.open("get", "php/get-content.php?ID=" + strInput, true);
    objRequest.send();
}

I hope you can help me to find out, where the issue is. If there are some better way to do this, let me know. I would be appreciate it. Thank you.

Share Improve this question edited Dec 18, 2018 at 19:03 Baku Bakar asked Dec 18, 2018 at 13:47 Baku BakarBaku Bakar 4622 gold badges9 silver badges26 bronze badges 3
  • you may want to use promises? – quirimmo Commented Dec 18, 2018 at 13:49
  • 1 You write HttpReuqest instead HttpRequest typo error ? – R3tep Commented Dec 18, 2018 at 13:50
  • 3 Possible duplicate of How do I return the response from an asynchronous call? – Heretic Monkey Commented Dec 18, 2018 at 13:57
Add a ment  | 

2 Answers 2

Reset to default 4

Do you mean an asynchronous callback? You'll want to wait until the server gives back the correct status and readyState.

Change this:

objRequest.onload = function() { var strResponse = this.responseText; return strResponse; };

To this:

objRequest.onreadystatechange = function() {

    // Waits for correct readyState && status
    if (objRequest.readyState == 4 && objRequest.status == 200) callBackMethod(objRequest.responseText);
 };`

and pass in a callback method as a second parameter to HttpRequest(strInput, callbackMethod) like so:

strResponseHttpRequest = HttpRequest(strInput, function(responseText) {
    console.log(responseText);
});

Also add callbackMethod as a parameter like so:

HttpRequest(strInput, callbackMethod)

Where is your strInput variable in your DoWork() function ing from? Maybe you forgot to define it somewhere? Could be for example:

function DoWork(strInput) {
    //some code

    var strResponseHttpRequest;
    strResponseHttpRequest = HttpRequest(strInput);

    console.log(strResponseHttpRequest);

    //continue working with the variable 'strResponseHttpRequest'
    //rest of my code
}

function HttpRequest(strInput) {
    function reqListener () {
        console.log(this.responseText);
    }
    var objRequest = new XMLHttpRequest(); //New request object

    objRequest.onload = function() {
        var strResponse = this.responseText;
        return strResponse;
    };

    objRequest.open("get", "php/get-content.php?ID=" + strInput, true);
    objRequest.send();
}

DoWork("yourIDvalue");
发布评论

评论列表(0)

  1. 暂无评论