0

I have two ajax requests to check if two images are available. The ajax calls are as follows:

                if (result['img_one'] !== '') {
                var src_one = "blah-blah-one.jpg";
                $.ajax({url: src_one,
                    success: function(data, textStatus) {
                        $("#profile-box").show();
                        $("#profile-img").attr("src", src_one);
                    },
                    error: function(jqXHR, textStatus, errorThrown) {
                        $('#profile-box').hide();
                    }
                });
            }
           if (result['img_two'] !== '') {
                var src_two = "blah-blah-two.jpg";
                $.ajax({url: src_two,
                    success: function(data, textStatus) {
                        $("#profile-box").show();
                        $("#profile-pic").attr("src", src_two);
                    },
                    error: function(jqXHR, textStatus, errorThrown) {
                        $('#profile-box').hide();
                    }
                });
            }

Now, I want to check if both the above requests succeed or not. If both of the requests return successfully and hence both images are available, I want to hide a div in my page.

How can I check that? I tried to set two variables when the requests succeeded, to check their values through an if-block and hide the div. however, they were not accessible outside the if-block. Any help will be much appreciated.

H. M..
  • 568
  • 6
  • 15

1 Answers1

-1

Try nested ajax call. Define the second ajax call in the success function of the first ajax call. And then define ur final message in the success of second ajax call

Rithik Banerjee
  • 447
  • 4
  • 16