相关疑难解决方法(0)

Ajax调用填充Typeahead Bootstrap

我想要做的是通过Ajax获取一个json对象,并使用一种值填充Bootstrap Typeahead.

这是我的代码:

nameTypeHead: function () {
        var _self = this,
            searchInput = $('#nameTypeHead'),
            arr = [];

        function getArray() {
            $.ajax({
                url: '/Home/AutoComplete',
                type: 'post',
                dataType: 'json',
                data: { searchText: searchInput.val() },
                success: function (data) {
                    $.each(data, function () {
                        arr.push(this.Name);
                    });
                    return arr;
                }
            });
        }

        searchInput.typeahead({
            source: getArray()
        });
    } 
Run Code Online (Sandbox Code Playgroud)

我收到arr为null的错误

我也试过.parseJSON()但没有成功:

$.each($.parseJSON(data), function () {
   arr.push(this.Name);
});
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能在Typeahed中显示我的Json对象的值Name?

如果在Ajax Success中我alert(JSON.stringify(data));正确地提醒我的Json对象.

ajax jquery json twitter-bootstrap

16
推荐指数
2
解决办法
5万
查看次数

标签 统计

ajax ×1

jquery ×1

json ×1

twitter-bootstrap ×1