2

I'm trying to return true or false depending on if the users token & role in the localstorage is ok. For some reason I'm getting undefined as a response... Could you guys help me?

  function isLogged(roleNeeded) {
    var token = "asd";//localStorage.getItem("token");

      function handleResponse(data){
          ajaxresponse = data;

          var val;
          var localRole = "s";//localStorage.getItem("role");

          val = (data.status === "ok" && localRole === roleNeeded);
          return val;
      }
      loadAjax(token, handleResponse)
  }
  function loadAjax(token, handleResponse) {
      $.ajax({
          url: 'http://localhost/users/login/verify',
          method: 'GET',
          headers: {token: token},
          success: function(data)
          {
            handleResponse(data)
          }
      });
  }
user121032
  • 161
  • 6
  • Note that you can't really return booleans, only strings, and `"false"` will be truthy etc. – adeneo Apr 02 '17 at 21:02
  • Thanks for noticing! Typo but it is not included in the code. – user121032 Apr 02 '17 at 21:02
  • @adeneo you can't return booleans? Can you explain? – jakeehoffmann Apr 02 '17 at 21:11
  • 1
    I just read the title "ajax request returning no boolean", and noted that you can't return a boolean to an ajax request, only strings. Looking at your code, that's not really your problem at all. You're returning `val` to `handleRespons()` but you're not doing anything with that at all? If you expected `handleResponse` to somehow magically make `loadAjax` return a boolean, you need to read [How do I return the response from an asynchronous call](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323) ? – adeneo Apr 02 '17 at 21:51

0 Answers0