ajax load()后调用函数

时间:2015-03-30 21:03:41

标签: javascript jquery ajax

我在ajax load()之后启动一个函数,但是我亲密的函数调用了一个无效的附加函数。如何在ajaxstuff()之后启动load()

function ajaxstuff(data) {
    $.ajax({
        type: "POST",
        url: "do-it.php",
        data: {data},
        success: function() {
            console.log('I got this far'); // this doesn't work / isn't called
        }
    });
}

function doit() {
    $('form').submit(function(e) {
        e.preventDefault();
        var formData = new FormData(this);
        console.log('I got this far'); // this works
        ajaxstuff(formData);
    }
}

$('.popup-loader').click(function() {
    $(this).append('<div class="popup"></div>');
    $('.popup').load('popup.php', function() {
        doit(); // this works
    }
});

2 个答案:

答案 0 :(得分:-2)

ajaxstuff函数中的语法错误。 data: {data},,可能应为data: data,

此外,当您将FormData对象传递给$ .ajax时,您必须指定processData: falsecontentType: false

$.ajax({
    type: "POST",
    url: "do-it.php",
    data: data,
    processData: false,
    contentType: false,
    success: function() {
        console.log('I got this far'); 
    }
});

答案 1 :(得分:-2)

检查控制台中的错误,同样在AJAX中添加async选项。将它设置为FALSE(&#34;将async设置为false意味着您调用的语句必须在函数中的下一个语句被调用之前完成&#34;):

function ajaxstuff(data) {
    $.ajax({
        async: false,
        type: "POST",
        url: "do-it.php",
        data: data,
        success: function(result) {
             console.log(result); //check in your console for errors
        }
    });
}