2013-01-11 17 views
0

我試圖做使用Ajax一個Twitter的引導typehead,但沒什麼happens.No錯誤沒有輸出 這裏是我的jQuery Ajax代碼jQuery的AJAX調用自舉不觸發typehead

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()沒有任何反應

+0

是否有alert()調用被觸發? – Alexander

+0

不,它不會觸發 – iJade

回答

0

你需要從一個通結果jax請求到源的第二個參數(這是一個回調來填充前面的類型)。一個人爲的例子:

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

我也試過這種方式,但仍然ajax沒有觸發 – iJade

0

嘗試添加async: false到$阿賈克斯 - 因爲JavaScript的機制是不同步的。 希望得到這個幫助