0

我尝试使用 Ajax 做一个 twitter 引导类型头,但没有发生任何错误。没有错误没有输出这是我的 jquery ajax 代码

    function CallData() {

    $('input.typeahead.local.remote').typeahead({
        source: function (typeahead,query) {
            $.ajax({
                type: "GET",
                url: "http://cs-api-sandbox.herokuapp.com/v1/challenges/search?keyword=r&callback=my_callback",
                jsonpCallback: "my_callback",
                dataType: "jsonp",
                error: function (xhr, errorType, exception) {
                    var errorMessage = exception || xhr.statusText;
                    alert("Excep:: " + exception + "Status:: " + xhr.statusText);
                }
            });
        }
    });

}

function my_callback(data) {
    alert('hi');
}

这是我的html代码

<input type="text" id="txt" runat="server" class="span4 typeahead local remote" placeholder="Search..." />

CallData()在每次按键时调用 ajax 函数,但没有任何反应

4

2 回答 2

0

您需要将 ajax 请求的结果传递给 source 的第二个参数(这是一个用于预先填充类型的回调)。一个人为的例子:

$('#mytypeahead').typeahead({
    source: function(query, process){
        $.ajax({
            url: '/some/url?query='+query,
            success: function(response){
                process(response);
            }
        });
    }
});
于 2013-01-11T23:25:53.527 回答
0

尝试添加async: false到 $.ajax - 因为 javascript 的机制是不同步的。希望这有帮助

于 2015-10-17T10:02:39.980 回答