1
function checkauth(){
       jQuery.getJSON("/users/checkauthjson", null, function(xhr){
           if(xhr.success){ return true;}else{ return false;}
            });
   }

Obviously, that does not work, but it illustrates what i want to achieve. I want checkauth to return a value which is checked against an ajax server script.

How do i do this?

1 Answer 1

1

getJson() is an asynchronous method by default, so you cannot rely on it without doing some adjustments. Use .ajax() function, setting async param to false, and then assign result to a var whose scope is in the checkAuth() function.

Something like this:

function ceckAuth() {
  var ret = false;
  $.ajax({
    url: '/users/checkauthjson',
    dataType: 'json',
    data: '',
    async: false,
    success: function(result) { ret = result.success; },
    error: ...
  });
  return ret;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Note that you page will be unresponsive whilst the AJAX request is in progress, because it's now synchronous.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.