1

I am calling a webservice in the validation method, but Validation method gets returned without completion of webservice,

Code>>

jQuery.validator.addMethod("name", function (value, element) {
    var validUsername = false;

    $.ajaxSetup({ "async": false });
    $.getJSON(serverName + '/handlers/qs-user.ashx?callback=?', {
        'username': $("#email").val(), 
        'op': 'GetMobileUser' 
        }, 
        function (data) {
            if (data.UserId == "null") {
                validUsername = true;
            } else {
                validUsername = false;
            }
        });

    $.ajaxSetup({ "async": true });
    var result = this.optional(element) || value == "" || validUsername;
    if (!result) {
        var validator = this;
        setTimeout(function () {
            validator.blockFocusCleanup = true;
            element.focus();
            validator.blockFocusCleanup = false;
        }, 1);
    }
    return result;
}, "That e-mail address has already been used.");

How to synchronise this web servicecall.. as

$.ajaxSetup({ "async": false });

is not working Thanks for looking into this matter. :)

  • Possible duplicate of [Is it possible to set async:false to $.getJSON call](http://stackoverflow.com/questions/2765411/is-it-possible-to-set-asyncfalse-to-getjson-call) – apsillers Jan 17 '13 at 15:12
  • However, in your case, you seem to be using JSONP, which cannot ever be synchronous. I advise you pull resources directly with Ajax (since Phonegap supports cross-domain Ajax), or restructure your code to be asynchronous (i.e., pass and fire a [callback function](http://recurial.com/programming/understanding-callback-functions-in-javascript/) instead of returning a value). – apsillers Jan 17 '13 at 15:17
  • 1
    It would probably be better to use the method within the jQuery Validation Plugin that is made for remote validation. http://docs.jquery.com/Plugins/Validation/Methods/remote – Kevin B Jan 17 '13 at 19:12

0 Answers0