如何使用jquery在ajax上设置延迟

how to set a deferred on ajax with jquery?

本文关键字:设置 延迟 ajax 何使用 jquery      更新时间:2023-09-26

让我从一些代码开始:

function sendAjax(data, type)
{
    $.ajax({
        type : "GET",
        dataType : "jsonp",
        url : rest + type,
        data : data,
    });
}

$.when(sendAjax(someData, 'url')).then(function(data){
    console.log(data); //undefined
});
$.when(sendAjax(someOtherData, 'url')).then(function(data){
    console.log(data); //undefined
});

我遇到的问题是data作为未定义的出现

如果我在$.ajax中使用success,则数据进入精细

这里的主要想法是,我应该写一次sendAjax()方法,并在应用程序中使用它,但我认为我没有正确设置

蚂蚁的想法?

您需要从sendAjax 返回$.ajax()返回的promise

function sendAjax(data, type)
{
    return $.ajax({
        type : "GET",
        dataType : "jsonp",
        url : rest + type,
        data : data,
    });
}