在Javascript变量中保存XMLHttpRequest.responseText

时间:2014-05-02 10:10:24

标签: javascript php ajax

这个问题之前曾被问过两次,但我无法让它继续工作。 在我完成帖子saving-xmlhttprequest-responsetext-in-a-variablestore-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);

        }
    }
}

有趣的是,console.log(hr.responseText);在继承功能&#39; onreadystatechange&#39;给了我绝对正确的结果。

但是,当我调用函数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") ...这个adice会以任何方式帮助吗?到目前为止,我无法让它工作......可悲的是:(


既然@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);
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个

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

那样电子邮件会改变并存储在功能范围内的电子邮件中并在功能执行时留在那里,希望它有帮助:)

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

返回回调(hr.responseText);

通过这种方式你返回一个函数,然后返回save..i祈祷有效..:)

如果有效,请告诉我