0

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]
Community
  • 1
  • 1
DevDave
  • 6,700
  • 12
  • 65
  • 99
  • 2
    AJAX is asynchronous, your `isAuthorised` function has exited long before return is called. You need to do all your processing within the callback function. – Rory McCrossan Jan 31 '13 at 13:20
  • Could you give an example Rory? – DevDave Jan 31 '13 at 13:21
  • possible duplicate of [Function inside jquery returns undefined](http://stackoverflow.com/questions/2504950/function-inside-jquery-returns-undefined) – Rory McCrossan Jan 31 '13 at 13:22
  • 1
    How about using `$.ajax()` and setting async parameter to false? – Jimbo Jan 31 '13 at 13:27
  • thanks for the suggestion Jimbo. I am trying to do what you suggested (see edit), could you tell me where I am going wrong? – DevDave Jan 31 '13 at 13:43
  • 1
    @Tyler see : http://api.jquery.com/deferred.done/ . `request.done()` returns a `Deferred` object. You can (but not very good) use : `request.done(function(data) { result = data; });` – Samuel Caillerie Jan 31 '13 at 13:47
  • Thanks Samuel! Post as answer and I'll accept – DevDave Jan 31 '13 at 14:17

2 Answers2

1

You should check the dataType parameter for the jQuery ajax call. Depending on the return value you want to have, it might be a good practice to explicitly set the dataType (xml, json, text, ...)

If you are giving back a json, it will be converted into a JavaScript object. From your alert I'm assuming that to be the case.

Try this mod for getting a string back:

var request = $.ajax({
    url: 'Action/Controller',
    type: 'post',
    dataType: 'text',
    async: false
});

alert(request.responseText); // [now it will be outputted as text]

In a sync ajax call request will be filled to be an object, so you have to use one of it's properties to extract the data.

kms
  • 389
  • 2
  • 6
0

You can't return from there. With ajax you have to structure your code differently, IE you can redirect in the callback

function isAuthorised() {
  $.post("Action/Controller", function (data) {
    if (!data) {
        //redirect
    }
  });
}

Anyway, checking authorisation with javascript doesn't make sense to me, you should do that prior to serving the page serverside

David Fregoli
  • 3,377
  • 1
  • 19
  • 40
  • +1 for answer as its probably the best thing to do, but I don't want to redirect in the callback – DevDave Jan 31 '13 at 13:47