0

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:

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):

<?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!

AndroidDev
  • 20,466
  • 42
  • 148
  • 239
  • You can't return a boolean, only the string `true`, but in your case PHP most likely returns the number `1` (or nothing) – adeneo Aug 06 '14 at 17:35
  • To be more specific, there is no special response type for booleans, XMLHttpRequest receives strings (or XML), and that's it. Return something you can actually check, like the string `true`. – adeneo Aug 06 '14 at 17:39
  • Thanks. I'm trying to use an existing php function that generates boolean return values. Sounds like I need to rewrite it for this purpose specifically to return a string. – AndroidDev Aug 06 '14 at 17:40
  • There is more wrong: `returnValue` will refer to the **callback**. You are not calling the `onreadystatechange` callback, hence you can't access its return value. You can assign a value to a free variable though and return that variable. However, making synchronous calls is generally a bad idea. See [How to return the response from an Ajax call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call). – Felix Kling Aug 06 '14 at 17:41

3 Answers3

0

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

then in your JS

session_verified = Boolean(xmlhttp.responseText);
gcr15
  • 117
  • 4
  • 1
    Using strings containing `false` or `true` is not a good idea: `Boolean("true") /* true */ ; Boolean("false") /* true */`. – Felix Kling Aug 06 '14 at 17:43
  • @FelixKling You are correct, I would just use the number 1 then. – gcr15 Aug 06 '14 at 17:45
  • @FelixKling - that's true, also `!!"false"` is a common mistake, but if you know what you're getting it's not an issue, as `result == 'true'` still works. – adeneo Aug 06 '14 at 17:45
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:

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

I hope it helps you.

Cheers,

bribeiro
  • 715
  • 5
  • 5
0

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

// 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();
hex494D49
  • 9,109
  • 3
  • 38
  • 47