13

我有一个ajax电话

    $.ajax({
        type: 'POST',
        url: 'addVideo',
        data: {
            video_title: title,
            playlist_name: playlist,
            url: id
            // csrfmiddlewaretoken: '{{ csrf_token }}',
        },
        done: bootstrap_alert.success('video saved successfully'),
        fail: bootstrap_alert.error('There were some errors while saving the video. Please try in a while')
    });

和行动

// setting up alerts on action
bootstrap_alert = function() {}
bootstrap_alert.success = function(message) {
  $('#feature').prepend('<div class="alert alert-success"><a class="close" data-dismiss="alert">×</a><span>'+message+'</span></div>');
}
bootstrap_alert.error = function(message) {
  $('#feature').prepend('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><span>'+message+'</span></div>');
}

当前端进行ajax调用时,我同时看到两个通知

video saved successfully
There were some errors while saving the video. Please try in a while

是我没有正确地进行ajax调用吗?

UPDATE
更改donesuccess导致相同的行为

// send the data to the server using .ajax() or .post()
        $.ajax({
            type: 'POST',
            url: 'addVideo',
            data: {
                video_title: title,
                playlist_name: playlist,
                url: id
                // csrfmiddlewaretoken: '{{ csrf_token }}',
            },
            success: bootstrap_alert.success('video saved successfully'),
            fail: bootstrap_alert.error('There were some errors while saving the video. Please try in a while')
        });

服务器响应是HTTP/1.0" 200 3200,我相信不fail应该被调用

4

5 回答 5

19

这些值应该是函数、回调。但是,您正在做的是立即给他们打电话。将您的回调包装在匿名函数中。

$.ajax({
  type: 'POST',
  url: 'addVideo',
  data: { video_title: title, playlist_name: playlist, url: id }
}).done(function(){
  bootstrap_alert.success('video saved successfully');
}).fail(function(){
  bootstrap_alert.error('There were some errors while saving the video. Please try in a while');
});
于 2012-08-06T23:25:59.277 回答
7

done总是被调用。这应该会发生。您应该在success属性中处理您的成功代码。

于 2012-08-06T22:55:38.327 回答
2

您需要将其包装在一个匿名函数中(正如其他人所指出的那样)。但是,我还没有看到有人提到原因(我认为值得一提)。

您需要这样做的原因是因为 javascript 将冒号右侧的评估分配给左侧。为了评估右侧,它需要首先运行您的函数。但是,如果您在右侧有一个匿名函数,它将函数本身定义为左侧引用的值(在 javascript 中,变量的值可以是函数)并将函数原样分配给左侧边(现在是对该函数的引用)。因此,它会延迟评估(运行函数),直到您的 ajax 调用完成。

于 2015-11-11T23:14:46.980 回答
1

像这样改变有效

// send the data to the server using .ajax() or .post()
        $.ajax({
            type: 'POST',
            url: 'addVideo',
            data: {
                video_title: title,
                playlist_name: playlist,
                url: id
                // csrfmiddlewaretoken: '{{ csrf_token }}',
            },
            success: function(response, textStatus, jqXHR){
                 // log a message to the console
                 console.log("Hooray, it worked!");
                 bootstrap_alert.success('video saved successfully');
            },

            // callback handler that will be called on error
            error: function(jqXHR, textStatus, errorThrown){
                // log the error to the console
                console.log("The following error occured: "+ textStatus, errorThrown);
                bootstrap_alert.error('There were some errors while saving the video. Please try in a while');
            },
        });
于 2012-08-06T23:30:31.880 回答
0

尝试donesuccessfail替换error? 当它们是回调挂钩时,您将它们用作选项。

于 2012-08-06T23:01:06.983 回答