1

I have a problem. I have following code in anonymous function:

        ...
        if (fillActyalizerForm(jQuery(this).attr('account_id'), jQuery(this).attr('currency_id')) == false) {
            return false;
        }
        jQuery("#actyalizer").slideToggle("slow");
        ...

THe problem is even if I return false, element slides down.

function fillActyalizerForm(account_id, currency_id) {
    formValues = {};
        jQuery.ajax({
            async: false,
            type: 'POST',
            url: "..",
            data: "{'accountId':" + account_id + ",'currencyId':" + currency_id + "}",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                if (data && data.hasOwnProperty("d")) {
                    data = data.d;
                }
                if (!data.Error) {
                    formValues = data;
                    delete formValues.Error;
                } else {
                    alert('<%=Resources.app.Actyalizer_AcLoadFail %>');
                    return false;
                }
            },
            error: function (data) {
                alert('<%=Resources.app.Actyalizer_AcLoadFail %>');
                return false;
            }
        });

So I am successfully get alert message, but it still slides down the elemtn.

3
  • 8
    are you sure it is getting to the return? Commented Feb 25, 2012 at 11:04
  • To expand on what @mindandmedia said, check for errors raised by the fillActyalizerForm function in your console. Commented Feb 25, 2012 at 11:21
  • @mindandmedia,@Rory McCrossan how to make sub-function return false? I'v tried try-catch, but it is failed. Commented Feb 25, 2012 at 11:38

1 Answer 1

1

The problem is that the fillActyalizerForm function executes the asynchronous .ajax method and then returns without specifying a return value (the default return value is then false).

I think you have two choices:

  1. Move the jQuery("#actyalizer").slideToggle("slow"); into the success function.
  2. Do a synchronous ajax request.

I would recommend option one. This can be achieved by passing your function as a parameter to fillActyalizerForm or physically move the code.

Sign up to request clarification or add additional context in comments.

3 Comments

i think ill wull use second option
@JakeFisher, be aware of the disadvantages when using synchronous AJAX calls, it blocks the UI thread! Research it and make sure that is what you really want.
Fourie yes you was right about first option. This was more preferrable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.