1

I cant seem to get this value to pass the isValid value back out of the ajax call in the snippet below:

    function isShortUrlAvailable(sender, args) {
        var isValid = false;
        $.ajax({
            type: "POST",
            url: "/App_Services/ShortUrlService.asmx/IsUrlAvailable",
            data: "{url: '" + args.Value + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                isValid = response.d;
            },
            error: function (msg) {
                isValid = false;
            }
        });

        args.IsValid = isValid;
    }

I'm sure its just something simple to do with closures that i'm overlooking. Can anyone help please?

Its for an asp.net custom validator.

This is what's happening:

  1. isValid is set to false in the first line
  2. .ajax() request fires correctly and if its valid returns true
  3. isValid is set to true (response.d) correctly
  4. when it comes back out to the last line it thinks isValid is false again
rtpHarry
  • 13,019
  • 4
  • 43
  • 64
  • 1
    short answer you can't return the value since ajax is asynchronous, you need to use a callback to solve this problem – Arun P Johny May 24 '13 at 10:56
  • You can't : your callback is executed only after the external function returns. – Denys Séguret May 24 '13 at 10:56
  • Can you share how the method is used? – Arun P Johny May 24 '13 at 10:57
  • I'm trying to use it in an `` but you pass the value back to the customvalidator by setting the args.IsValid and its not getting it. – rtpHarry May 24 '13 at 11:35
  • I seem to have solved it with async: false but I've read this is deprecated in future versions of jQuery and also "a-bad-idea" – rtpHarry May 24 '13 at 11:36
  • Thanks for closing my question, I solved it and was most of the way through replying to my own question with examples and reasons behind the issue. Now I can't post it for others to learn from. If the other "duplicates" answered my question I would have said that wouldn't I! :( – rtpHarry May 24 '13 at 11:46

1 Answers1

1

AJAX methods are asynchronous meaning that you're value is set to false, the AJAX call is started, but whilst it's happening the args.IsValid line is called. Just remove the variable and set the args.IsValid value in each scenario:

function isShortUrlAvailable(sender, args) {
    $.ajax({
        type: "POST",
        url: "/App_Services/ShortUrlService.asmx/IsUrlAvailable",
        data: "{url: '" + args.Value + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            args.IsValid = response.d;
        },
        error: function (msg) {
            args.IsValid = false;
        }
    });
}
Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
  • Thanks but that didn't solve it. I was setting it that way for a while and it still didnt pass it back up the chain. I'd only changed it to the posted code just before as I read another example that I thought might be the solution. – rtpHarry May 24 '13 at 11:40