首页 > javascript > 如何在HTTP请求后调用回调方法

如何在HTTP请求后调用回调方法 (How to call a callback method after HTTP request)

问题

我的代码在main函数中运行。我的代码的一部分是使用在函数中定义的参数发出HTTP请求,然后将响应写入新变量并稍后使用它。

我想在主函数之外用HTTP Request排除这些步骤,只需调用函数并在变量中写入响应。

不幸的是我尝试过,但它不起作用。

Error: variable is undefined

我的代码:

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();
}

我希望你能帮助我找出问题所在。如果有更好的方法可以做到这一点,请告诉我。我会很感激的。谢谢。

解决方法

你的意思是异步回调吗?您将要等到服务器返回正确的状态和readyState。

改变这个:

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

对此:

objRequest.onreadystatechange = function() {

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

并传入一个回调方法作为第二个参数,HttpRequest(strInput, callbackMethod)如下所示:

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

还添加callbackMethod如下参数:

HttpRequest(strInput, callbackMethod)

问题

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.

解决方法

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)
相似信息