1
phpから先読みにデータの配列を渡したいが、それが機能していない、私は間違っていることを知らない。私は他の質問をチェックしましたが、解決策を得ることができませんでした。私は、コンソール上の応答を表示してみました、それはphpで先制型にjsonデータを渡すことができません
PHP
$resp = array();
$states = $this->cj_model->list_all_states();
foreach($states as $row){
$resp[] = $row['name'];
}
echo json_encode($resp);
JS
var substringMatcher = function(strs) {
return function findMatches(q, cb) {
var matches, substringRegex;
// an array that will be populated with substring matches
matches = [];
// regex used to determine if a string contains the substring `q`
substrRegex = new RegExp(q, 'i');
// iterate through the pool of strings and for any string that
// contains the substring `q`, add it to the `matches` array
$.each(strs, function(i, str) {
if (substrRegex.test(str)) {
matches.push(str);
}
});
cb(matches);
};
};
AJAX
function get_states()
{
$.ajax({
url: 'country/get_states/',
dataType: 'json',
type: 'get',
async: true,
success: function(response){
return (response);
},
error: function(jqxhr, textStatus, error){
console.log(error);
}
});
}
var states = get_states();
$('.typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'states',
source: substringMatcher(states)
});
主な鉱山は、サーバーの成功コールバックの後に先行入力を活性化することである –
何それはコード 'ソースで使用されているAJAX呼び出しからの応答、およそ:substringMatcher(レスポンス)'病気(このおかげで – 4Jean
単純な変更変数substringMatcherを試してみてください状態)with substringMatcher(応答) –