从Ajax查询返回字符串值

时间:2016-01-14 14:02:50

标签: jquery ajax

这可能是缺乏真正理解jQuery,但我试图创建一个函数来调用一个返回单个字符串值的处理程序(sql查询中的几个字符串的串联)。然后将这些值组合并保存为一个字符串值,并返回并存储为变量供以后使用。

我可以确认hanlder的作品。内部displayList变量确实收集了正确的信息。这不是问题。问题是如何将此输出值返回到原始调用。

以下是一个示例。

$(document).ready(function () {
    // Get Drop Down List Options
    var listValues = populateDropDown(9);

    // Returns "" Instead Of Full String
    alert(listValues);         
});

function populateDropDown(iCategoryID) {

        var displayList = "";

        $.ajax({
            url: "queries/dh_getListInfo.ashx",
            contentType: "application/json; charset=utf-8",
            cache: false,
            dataType: "json",
            data: {
                iCategoryID: iCategoryID,
            },
            responseType: "json",
            success: function (response) {

                // Loop Through Values And Build String
                if (response.length != 0) {
                    $.each(response, function (index, row) {
                        for (var index in row) {
                            displayList += "<option value=\"" + row[index].value + "\">" + row[index].value + "</option>";
                        }
                    });
                }

            },
            error: function () {
                alert('fail');
            },
        });

        return displayList;

};

感谢任何帮助。

0 个答案:

没有答案