AJAX函数返回值

时间:2017-07-28 15:56:24

标签: javascript jquery ajax

我有以下功能,但似乎无法让returnedData = result成功设置该值。如果我在同一位置添加alert(result),则弹出窗口会显示我正在寻找的字符串。

我有什么东西在这里失踪吗?我曾经想过,因为我在函数之外声明了变量returnedData,它随处可见?

function AJAXprocesstwoVariables(Var1, Var2) {
    var V1 = Var1,
        V2 = Var2;
        var returnedData;
    $.post(
        processinglocation, { 
            data1: V1,
            data2: V2 
        },
        function (result) {
            returnedData = result; // *<- this doesn't work*
            // alert(result); // *<-this works*
        }
    );
    return returnedData;
}

var ReturnedInfo = AJAXprocesstwoVariables(Var1, Var2);
$('body').append(ReturnedInfo);

1 个答案:

答案 0 :(得分:0)

AJAXprocesstwoVariables将异步运行。运行该行后,请求将在后台进行,您将立即点击returnedData的返回行。您在$.post的成功功能中设置之前尝试使用SELECT categories.name, DATE_TRUNC('week', created_at) AS week COUNT(*) AS topic_count, FROM topics, categories WHERE topics.deleted_at IS NULL AND AND topics.category_id = categories.id AND categories.name LIKE 'Category Name' GROUP BY categories.name, DATE_TRUNC('week', created_at) ORDER BY categories.name, week DESC