拯救XMLHttpRequest.Javascript变量中的responseText

save XMLHttpRequest.responseText in a Javascript variable

本文关键字:responseText 变量 XMLHttpRequest Javascript 拯救      更新时间:2023-09-26

这个问题之前被问了2次,我无法让它工作。在我阅读了save - XMLHttpRequest -responsetext-in-a-variable和store-xmlhttprequest-responsetext-as-variable两篇文章之后,我了解了如何调用回调函数,因为XMLHttpRequest是异步的。(似乎是显而易见的)

因此,我创建了我的phpfile checkemail。php:
<?php
if(isset($_POST['email'])){ //if we get the email sucessfully
    $email = mysqli_real_escape_string($db, $_POST['email']);
    if(!empty($email)){
        $email_query = mysqli_query($db, "SELECT COUNT(`email`) FROM `users` WHERE `email`='$email'");
        $email_result = mysqli_fetch_row($email_query);
        if($email_result[0]==0){
            //haven't found the email in the DB
            echo "0";
        } else{
            //found the email in the DB!
            echo "1";
        }
    }
}
?>

所以我的结果是0或1,无论电子邮件是否已经在数据库中。

给我的Javascript代码和我的问题这个线程:

function callback(responseText){
    var save = responseText;
    return save;
}
function checkemail(email){
    var hr = new XMLHttpRequest();
    hr.open("POST", "./inc/func/checkemail.php", true);
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    // Send the data to PHP
    hr.send('email='+email);
    // Access the onreadystatechange event for the XMLHttpRequest object
    hr.onreadystatechange = function() {
        if(hr.readyState == 4 && hr.status == 200) {
            console.log(hr);
            console.log(hr.responseText);
            callback(hr.responseText);
        }
    }
}

有趣的是,继承函数'onreadystatechange'中的console.log(hr.responseText);给了我绝对正确的结果。

但是,当我调用函数checkemail('test@test.com')函数回调不调用,或者我误解了一些东西吗?

帮助将是非常感激的。提前感谢


我也尝试过这个,因为它对我来说更合乎逻辑(当同一块代码中的console.log已经给了我正确的结果…):

    hr.onreadystatechange = function() {
        if(hr.readyState == 4 && hr.status == 200) {
            console.log(hr);
            console.log(hr.responseText);
            return hr.responseText;
        }
    }

在另一篇文章中,我看到了语句

如果你改变你的函数调用hr.open("GET", url, false);,这应该使你的调用同步和JavaScript将等待,直到收到响应。

当然,正如我之前说过的,我想调用if(checkemail('test@test.com')=="1")…这个建议会有帮助吗?到目前为止,我还不能让它工作。可悲的是:(


现在@Hawk给了我一些提示,我已经尝试了更多的方法来让我的函数返回真或假。

但是我想知道如果我的函数不能调用函数callback

function callback(responseText){
    var save = responseText;
    return save;
}
function checkemail(email){
    var email = email;
    var hr = new XMLHttpRequest();
    hr.open("POST", "./inc/func/check_email.php", true);
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    // Send the data to PHP
    hr.send('email='+email);
    // Access the onreadystatechange event for the XMLHttpRequest object
    hr.onreadystatechange = function() {
        if(hr.readyState == 4 && hr.status == 200) {
            email=hr.responseText;
            //return email
            document.getElementById('status').innerHTML="email is used already: "+email;
            return callback(email);
        }
    //return callback(email);
    }
}

在函数checkemail之外定义函数回调是否有区别,或者我必须在函数checkemail中定义它,就像这样:

function checkemail(email){
    function callback(responseText){
        var save = responseText;
        return save;
    }
    var email = email;
    var hr = new XMLHttpRequest();
    hr.open("POST", "./inc/func/check_email.php", true);
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    // Send the data to PHP
    hr.send('email='+email);
    // Access the onreadystatechange event for the XMLHttpRequest object
    hr.onreadystatechange = function() {
        if(hr.readyState == 4 && hr.status == 200) {
            email=hr.responseText;
            //return email
            document.getElementById('status').innerHTML="email is used already: "+email;
            return callback(email);
        }
    //return callback(email);
    }
}

try this

function checkemail(email)
{
var email = email;
//code
if( hr.readystatechange == 4 & hr.status == 200 )
{
email = hr.responseText;
}
}

这样email就会改变并存储在函数作用域中的email中,并在函数执行时保持在那里,希望它能有所帮助:)

尝试将调用回调(hr.responsetext)的部分更改为此

返回回调(hr.responseText);

,这样你返回一个函数,反过来返回保存…我祈祷工作…:)

请让我知道它是否有效