I am having a problem with the following Jquery code:
function isAuthorised() {
$.post("Action/Controller")', function (data) {
return data;
});
}
I have tested the return value from my MVC
controller and that is fine. But when I check the authorisation with the following:
function onLoad() {
var result = isAuthorised();
alert(result); // undefined
if (!result)
// redirect
}
the returned value is undefined. I have seen a similar post here and think this may be a problem with encapsulation
, but the difference being that I need to return the value that is retrieved from JQuery.post()
.
Edit: Ok, I don't want to do the redirect inside the callback, so I am now attempting to return the value by using .Ajax and setting Async to false. I am now returning [Object object]
with the code below. Could someone explain the difference now I am setting async to false and help me return the value?
var request = $.ajax({
url: 'Action/Controller',
type: 'post',
async: false
});
var result = request.done(function (data) {
return data;
});
alert(result); // [Object object]