简体   繁体   中英

Ajax success is not working on nested .ajax

I am having an ajax that is within another.ajax. While the success code is triggered on the fist ajax ( url: "get_hours.php" ), for some reason it is not triggered in the second case ( url: "send.php" ). I can confirm that the code within "send.php" is executed correctly, however self.close(); is not working. Also, if I execute the self.close(); before the second.ajax it does work. In addition, I have tried to add an alert in the second success, which is not firing as well.

Here is my code

$('#mydiv').datepicker({
            weekStart: 1,
            language: "bg"
        });

        var fid = $('#fid').val();
        var procedure = $('#procedure').val();

        $('#mydiv').datepicker().on('changeDate', function() {
            var full_date = $("#mydiv").datepicker('getDate');
            var year_month = full_date.getFullYear() + '-' + (full_date.getMonth() + 1); // Y-n-j in php date() format
            var day = '0'+full_date.getDate(); // Y-n-j in php date() format
            var date = year_month+'-'+day.slice(-2);

            $('#text').val(date);
            $('#result').html('Date: ' + date);
            $('#container').fadeOut().empty();

            var time ='';
            $('#submit').prop('disabled', true).css('display', 'none');
            $('#loading').css('display', 'block');

            $.ajax({
                url: "get_hours.php",
                method: "POST",
                data: {
                    date: date,
                    fid: fid,
                    procedure: procedure
                },
                success: function(data) {
                    setTimeout(function() {
                            $('#container').empty();
                            $('#loading').css('display', 'none');
                            $('#container').append(data).hide().fadeIn(500);
                            $('.cell').on('click', function() {
                            $('.cell').removeClass('select');
                            $(this).addClass('select');
                            var time = $(this).text();
                            var date = $('#text').val();

                if (time != '') {
                  $('#submit').prop('disabled', false).css('display', 'block');
                } else {
                  alert(date + time);
                }

                            $('#result').html('Date: ' + date + ', Time: ' + time);
                            var account = "<? echo $account; ?>";
                            var userid = "<? echo $userid; ?>";
                            $('#submit').click(function() {

                                //self.close();

                                var date = $('#text').val();
                                $.ajax({
                                    url: "send.php",
                                    method: "POST",
                                    data: {
                                        date: date,
                                        time: time,
                                        account: account,
                                        userid: userid
                                    },
                                    success: function(data_sent) {
                                        self.close();
                                    }
                                });
                            });
                        });
                    },500);   }
            });
        });

Tried to search for an answer here, but could find anything that could help me really. So, any ideas are much appreciated. Thanks!

Ok, it seems that the success is not working only with send.php file. If I use another file it works fine. I guess it is something with the curl response I have in the send.php file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM