从JS AJAX函数调用捕获布尔返回值

时间:2023-01-17 00:00:33

Really simple javascript function that does an AJAX call. When the function returns, it returns with a boolean value (not a string). Right now, for testing purposes, I have it set to always return 'true'. Problem is that I don't seem to be able to capture this value so that I can evaluate it. Here is the code:

非常简单的javascript函数,可以进行AJAX调用。当函数返回时,它返回一个布尔值(不是字符串)。现在,出于测试目的,我将其设置为始终返回'true'。问题是我似乎无法捕获此值以便我可以对其进行评估。这是代码:

function verifySession() {

    var xmlhttp = new XMLHttpRequest();

    var returnValue = xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {

            // this is wrong, but I don't know how to alter it for boolean values
            session_verified = xmlhttp.responseText;

            // this outputs as empty, even though the return value is true
            console.log(session_verified); 

            if (!session_verified) {
                console.log("false value returned");
                return false;
            } else {
                console.log("true value returned");
                return true;
            }
        }
    }

    xmlhttp.open("GET", "/scripts/session_verifier.php", false);
    xmlhttp.send();
    return returnValue;
}

session_verifier.php basically looks like this (again, grossly simplified for testing purposes):

session_verifier.php基本上看起来像这样(再次,为了测试目的而大大简化):

<?php
return true;
?>

I've used this many times for functions that return strings, but this time I need it to return a boolean value. How can I capture its return value? Thanks!

我已经多次使用这些函数来返回字符串,但这次我需要它来返回一个布尔值。我如何获取其返回值?谢谢!

3 个解决方案

#1


0  

In your php script. Try returning the string value true ("true"), or the number 1

在你的PHP脚本中。尝试返回字符串值true(“true”)或数字1

then in your JS

然后在你的JS中

session_verified = Boolean(xmlhttp.responseText);

#2


0  

Testing your code I've noticed that the result is empty, as you can see here:

测试你的代码我注意到结果是空的,你可以在这里看到:

readyState: 4
response: ""
responseText: ""
responseType: ""
responseXML: null

But if you change a bit your PHP, you can see the result:

但是如果你改变一下你的PHP,你可以看到结果:

header('Content-Type: application/json');
echo json_encode(array(
    'success' => true,
));

I hope it helps you.

我希望它对你有所帮助。

Cheers,

#3


0  

Since the XMLHttpRequest response might be a text or XML, you can handle the Boolean return value this way.

由于XMLHttpRequest响应可能是文本或XML,因此您可以通过这种方式处理布尔返回值。

// PHP
function your_function(){
  if($something == TRUE){
    return 1;
  }else{
    return 0;
  }
}

// JavaScript
xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onreadystatechange = function () { 
  if (xhr.readyState == 4 && xhr.status == 200) {
      if(parseInt(xhr.responseText)){
        // true
      }else{
        // false
      }
  }
};
xhr.send();

#1


0  

In your php script. Try returning the string value true ("true"), or the number 1

在你的PHP脚本中。尝试返回字符串值true(“true”)或数字1

then in your JS

然后在你的JS中

session_verified = Boolean(xmlhttp.responseText);

#2


0  

Testing your code I've noticed that the result is empty, as you can see here:

测试你的代码我注意到结果是空的,你可以在这里看到:

readyState: 4
response: ""
responseText: ""
responseType: ""
responseXML: null

But if you change a bit your PHP, you can see the result:

但是如果你改变一下你的PHP,你可以看到结果:

header('Content-Type: application/json');
echo json_encode(array(
    'success' => true,
));

I hope it helps you.

我希望它对你有所帮助。

Cheers,

#3


0  

Since the XMLHttpRequest response might be a text or XML, you can handle the Boolean return value this way.

由于XMLHttpRequest响应可能是文本或XML,因此您可以通过这种方式处理布尔返回值。

// PHP
function your_function(){
  if($something == TRUE){
    return 1;
  }else{
    return 0;
  }
}

// JavaScript
xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onreadystatechange = function () { 
  if (xhr.readyState == 4 && xhr.status == 200) {
      if(parseInt(xhr.responseText)){
        // true
      }else{
        // false
      }
  }
};
xhr.send();