0

I'm having a problem with the following code. It works fine up until the point where it starts to apply changes to the html that was loaded in the first ajax function.

Basically I'm trying to nest one ajax call inside another.

$(document).on("click", '#advertiser-email-submit', function(event) {
    $(this).blur();
    var email = $("#advertiser-email").val();

    $(".marketing").hide().html("");
    $("#stats-container").hide().html("");
    $("#advertiser-container").hide().html('<div style="height:64px;"><div class="col-md-5"></div><div class="col-md-1"><img src="https://www.example.com/images/spinner_white_green.gif"></div><div class="col-md-5"></div></div>').show();

    $.post("https://www.example.com/advertisers/interface/email/submit", {
            email: email
        },
        function(data, status) {
            if (status == "success") {
                if (data.length > 0) {
                    //Load advertiser interface
                    $("#advertiser-container").hide().html(data).show();

                    //Load exchange visits list
                    var advertiser_id = $("#advertiser-overview").data("advertiser-id");

                    //THIS IS WHERE IT STARTS TO FAIL

                        $("#exchange-visits-container").hide().html('<div style="height:64px;"><div class="col-md-5"></div><div class="col-md-1"><img src="https://www.example.com/images/spinner_black_white.gif"></div><div class="col-md-5"></div></div>').show();
                        $.post("https://www.example.com/advertisers/interface/exchange/visits/list", {
                                advertiser_id: advertiser_id
                            },
                            function(data, status) {
                                if (status == "success") {
                                    if (data.length > 0) {
                                        $("#exchange-visits-container").hide().html(data).show();
                                    }
                                }
                            });
                }
            }
        });
    return false;
});
14
  • 2
    In what way does this actually fail? What happens? When you debug it, where does the observed behavior differ from the expected behavior? Commented Feb 29, 2016 at 17:48
  • 2
    Remove the inner $(function() { ... }) wrapping your code. It is a document ready handler. Commented Feb 29, 2016 at 17:48
  • @David it just does nothing Commented Feb 29, 2016 at 17:50
  • 1
    Use the browser's network monitor to examine these AJAX requests. What is the response from the first request and is the second request made? Commented Feb 29, 2016 at 17:54
  • 1
    You shouldn't re-use the same variable names in the second callback, either - if only for the sake of clarity. Commented Feb 29, 2016 at 17:55

1 Answer 1

1

Your wrapping the inner function in a document ready instead of just calling the ajax

$(document).on("click", '#advertiser-email-submit', function(event) {
    $(this).blur();
    var email = $("#advertiser-email").val();

    $(".marketing").hide().html("");
    $("#stats-container").hide().html("");
    $("#advertiser-container").hide().html('<div style="height:64px;"><div class="col-md-5"></div><div class="col-md-1"><img src="https://www.example.com/images/spinner_white_green.gif"></div><div class="col-md-5"></div></div>').show();

    $.post("https://www.example.com/advertisers/interface/email/submit", {
        email: email
    },
    function(data, status) {
        if (status == "success") {
            if (data.length > 0) {
                //Load advertiser interface
                $("#advertiser-container").hide().html(data).show();

                //Load exchange visits list
                var advertiser_id = $("#advertiser-overview").data("advertiser-id");

                    var template = $('#templateId').removeAttr('id');
                    $("#exchange-visits-container").hide().html(template ).show();
                    $.post("https://www.example.com/advertisers/interface/exchange/visits/list", {
                        advertiser_id: advertiser_id
                    },
                    function(data, status) {
                        if (status == "success") {
                                if (data.length > 0) {
                                $("#exchange-visits-container").hide().html(data).show();
                            }
                        }
                    });

            }
        }
    });
   return false;
});

Also it would be easier to debug and clean you code up if you used a template like this:

<div id="templateId" style="height:64px;"><div class="col-md-5">
   </div><div class="col-md-1"><img src="https://www.example.com/images/spinner_black_white.gif"></div>
   <div class="col-md-5"></div>
</div>

then you could set you html easier like this:

 var template = $('#templateId').removeAttr('id');
 $("#exchange-visits-container").hide().html(template ).show();

this is just an example so you should probably hide the template and then also make it visible

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

5 Comments

Thanks but it does the same thing if I do that. Nothing happens.
Are you sure it's returning success? maybe add some error handlers
I don't think it's even making the call. $("#exchange-visits-container").hide().html() even this line isn't executing.
check to make sure your server is returning the correct html data. You should just add some console.logs so you won't have to debug
@AmyNeville try the template pattern later too it will make all those nasty string html issues go away and it will be easier to maintain

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.