2014-06-26 49 views
2

如果數據在服務器或服務器中不可用或者服務器數據爲空,則JavaScript會拋出異常obj is null。如何捕捉這個異常?什麼是處理typeahead/Bloodhound代碼拋出異常的正確方法?

在stackoverflow中搜索沒有給出任何結果。

下面的代碼:

var test1 = new Bloodhound({ 
    datumTokenizer: function (d) { 
     return Bloodhound.tokenizers.whitespace(d.value); 
    }, 
    queryTokenizer: Bloodhound.tokenizers.whitespace, 
    remote: { 
     url: "http://URL?query=", 

     replace: function(url, query) { 
      return url + "" + query; 
     } 
    } 
}); 

test1.initialize(); 

$('#idOfAutoCompleteTextBox').typeahead({ 
    hint: true, 
    highlight: true, 
    minLength: 1 
}, 
{ 
    name: 'test1', 
    displayKey: 'value', 
    source: test1.ttAdapter() 
}); 

回答

0

我不知道這是你的問題的解決方案,但目前我處理空的結果(預輸入0.11.1)時具有相同的JavaScript異常。

在數組爲空的遠程URL文件中,回顯未使用JSON格式的內容。

的JavaScript

var termekek = new Bloodhound({ 
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('megnev'), 
    queryTokenizer: Bloodhound.tokenizers.whitespace, 
    remote: { 
     url: _BASE_ + 'components/com_rexwebshop/include/keres.php?q=%QUERY', 
     wildcard: '%QUERY' 
    } 
    }); 

    jQuery('.typeahead').typeahead(null, { 
    name: 'rs3ws-termekek', 
    display: 'megnev', 
    source: termekek, 
     templates: { 
     empty: [ 
      '<div class="empty-message">', 
      '<? echo JTEXT::_('Nincs találat') ?>', 
      '</div>' 
     ].join('\n'), 
     suggestion: Handlebars.compile('<div><strong>{{megnev}}</strong> – {{bruttoar}}</div>') 
     } 
    }); 

PHP

// [collecting data] 

    if (count($rows) == 0) 
    echo "_NOT_FOUND_"; 
    else 
    echo json_encode($rows, JSON_UNESCAPED_SLASHES); 
+0

你能如何,這可能有助於解決樓主的問題,這個問題詳細點嗎?我不太關注你。 – Kmeixner

+0

遠程PHP的來源不存在的問題,所以我不能給出一個確切的解決方案 –

相關問題