【问题标题】:jquery ajax call not triggering in bootstrap typeheadjquery ajax调用未在引导输入中触发
【发布时间】:2013-01-11 23:21:18
【问题描述】:

我尝试使用 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..." />

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

【问题讨论】:

  • 是否会触发任何alert() 调用?
  • 不,它不会被触发

标签: javascript jquery ajax bootstrap-typeahead


【解决方案1】:

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

$('#mytypeahead').typeahead({
    source: function(query, process){
        $.ajax({
            url: '/some/url?query='+query,
            success: function(response){
                process(response);
            }
        });
    }
});

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2012-07-17
      • 1970-01-01
      • 1970-01-01
      • 2013-03-20
      • 2011-09-30
      • 2012-05-04
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      相关资源
      最近更新 更多