-1

I have this code..

if (!checkIfCustomerIsValid(event)) {
        event.preventDefault();
        return false;
    }
else {
   AddCustomer();
}

function checkIfCustomerIsValid(event) {
    if ($('#txtCName').val() == '') {
        alert('Please enter a valid value for customer name!');
        return false;
    }
    if ($('#txtCAddress').val() == '') {
        alert('Please enter a valid value for customer address!');
        return false;
    }

}

It returned fine, until then, but I added a new check and its not returning anything.

function checkIfCustomerIsValid(event) {

  // code that was already there, the name and address check

  var _mobNo;
    if ($('#txtMobile').val() == '') return false;

    var _unq = $.ajax({
        url: '../Autocomplete.asmx/IsMobileUnique',
        type: 'GET',
        contentType: 'application/json; charset=utf8',
        dataType: 'JSON',
        data: "mobileNo='" + $('#txtMobile').val() + "'",
        async: false,
        timeout: 2000,
        success: function (res) { if (res.d) return false; else return true; },
        error: function (res) { alert('some error occurred when checking mobile no'); }
    }),chained = _unq.then(function (data) { if (data.d == false) { alert('mobile no already exists!'); $('#txtMobile').focus(); return false; } return true; });

}

If mobile no is not unique the alert shows fine that mobile no is not unique, but when it is unique the code doesn't go into AddCustomer (in the else part)??? Is it not returning true? Why is it not going into AddCustomer???

Razort4x
  • 3,296
  • 10
  • 50
  • 88
  • possible duplicate of [Variable doesn't get returned from AJAX function](http://stackoverflow.com/questions/12475269/variable-doesnt-get-returned-from-ajax-function) – Denys Séguret Apr 05 '13 at 06:02
  • possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Qantas 94 Heavy Feb 27 '14 at 13:08

3 Answers3

0

With your new test, checkIfCustomerIsValid is asynchronous. There is no way for it, even with deferred, to directly return the result of a distant call.

The simplest here would be to pass a callback to your checkIfCustomerIsValid function or to return the promise from the function. As you mix synchronous and asynchronous tests, the best would be to pass a callback to checkIfCustomerIsValid.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • I have set 'async: false' so how is it still asynchronous?? – Razort4x Apr 05 '13 at 06:02
  • Never use `async` : it blocks the UI. – Denys Séguret Apr 05 '13 at 06:03
  • I know, but I am just pointing out that you said I have set 'async: false' so it shouldn't be asynchronous now? – Razort4x Apr 05 '13 at 06:04
  • The use of async with deferred is deprecated. See http://api.jquery.com/jQuery.ajax/ – Denys Séguret Apr 05 '13 at 06:05
  • Regardless of whether it's blocking or not, you're still using an anonymous function and returning in an anonymous function only returns out of that anonymous function and not checkIfCustomerIsValid. This is because callbacks like your anonymous function in _.unq.then are ran somewhere else, outside of your function and it wouldn't make logical sense for them to be capable of returning a value to your original method. – Abe Haskins Apr 05 '13 at 06:06
0

You're correct, there is not a scenario where checkIfCustomerIsValid would return true. This is because you're attempting to return true from an anonymous function (i.e. a callback after the ajax request). When you return true from

    chained = _unq.then(function (data) { if (data.d == false) { alert('mobile no already exists!'); $('#txtMobile').focus(); return false; } return true; });

You're only returning from that anonymous function, not from checkIfCustomerIsValid. Solving this problem is not completely straight forward and is an issue created from the nature of asynchronous calls. The most common solution to this is to pass a callback to your asynchronous call. Here is a fiddle which implements this.

http://jsfiddle.net/p3PAs/

Abe Haskins
  • 1,378
  • 1
  • 7
  • 9
0

Ajax is asynchronous and won't block, so the return value is most likely undefined. You can modify the code to something like the following:

success: function (res) { if (res.d) callRoutineToAddCustomer(); },
Anthon
  • 69,918
  • 32
  • 186
  • 246
cody
  • 1