如果@Ajax调用成功,则调用Success脚本

时间:2014-07-24 14:08:01

标签: javascript jquery

我有以下包含ajax调用的javascript: -

$.ajax({
    type: "POST",
    url: '@Url.Action("DeleteSelected"),
    data: { ids: boxData.join(",") }
  })
    });
但是有没有办法调用javaScript函数是上面的Ajax调用成功了吗? 感谢

2 个答案:

答案 0 :(得分:3)

function mySuccessFunction() {
    alert('success');
}

$.ajax({
    type: "POST",
    url: '@Url.Action("DeleteSelected")',
    data: { 
        ids: boxData.join(",") 
    },
    success: function(data) {
        // your code if AJAX call finished successfully
        // call your function that already loaded from here:
        mySuccessFunction();
        // you can also process returned data here            
    }
});

答案 1 :(得分:0)

您可以使用三个特定处理程序来处理使用AJAX,.success(),.。done()和.fail()返回的信息。使用这些方法,您的代码可能如下所示:

$.ajax({
    type: "POST",
    url: '@Url.Action("DeleteSelected"),
    data: { ids: boxData.join(",") }
}).success(function(data) {
    // This callback is executed ONLY on successful AJAX call.
    var returnedJSON = data; // The information returned by the server
    yourSuccessFunction();
}).done(function() {
    // This callback is ALWAYS executed once the AJAX is complete
    yourDoneFunction();
}).fail(function() {
    // This callback is executed ONLY on failed AJAX call.
});

另见:jQuery AJAX Documentation