为什么在这个函数中返回jquery AJAX promise不能给我数据呢?

125*_*748 1 javascript ajax jquery deferred jquery-deferred

这个AJAX适用于jsfiddle

var a = $.ajax({
    url: "/echo/json/",
    type: "post",
    data: {
        json: JSON.stringify({
            a: true
        })
    },
    dataType: "json"
});

a.done(function (data) {
   console.log(data);
});
Run Code Online (Sandbox Code Playgroud)

当我创建a函数并返回AJAX承诺时,为什么它不起作用?

var a = function () {
    return $.ajax({
        url: "/echo/json/",
        type: "post",
        data: {
            json: JSON.stringify({
                a: true
            })
        },
        dataType: "json"
    });
}

a.done(function (data) {

    console.log(data);

});
Run Code Online (Sandbox Code Playgroud)

这不是正确的语法吗?好吧,显然不是,但我怎样才能在函数中构建AJAX请求?小提琴

Bar*_*mar 5

既然a是一个函数,你必须调用它:

a().done(function(data) {
    console.log(data);
});
Run Code Online (Sandbox Code Playgroud)