如何从控制器获得 JSON 结果到 JavaScript
How do I get JSON result from controller to JavaScript
我在控制器中有一个查询结果,我将其编码为 json
。我需要将它传递给 javascript,因为我想使用 typeahead js
来自动完成搜索输入。这是我的尝试:
我的控制器(Admin.php):
public function promotion()
{
$this->db->distinct();
$this->db->select("from_email");
$this->db->from('booking');
$query2 = $this->db->get();
$results = $query2->result_array();
$data['from_email'] = json_encode($results);
$this->template->render('admin/promotion',$data,'admin');
}
我的输入搜索(根据数据库中的电子邮件地址进行搜索):
<input type="text" name="email" class="form-control input-lg typeahead typeahead-from-email" id="email" required="required" autocomplete="off" tabindex="1" />
Javascript :
var emails = new Bloodhound({
datumTokenizer: function (d) {
return Bloodhound.tokenizers.whitespace('from_email');
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: base_url + 'admin/promotion'
});
$('.typeahead-from-email').typeahead({
hint: true,
highlight: true,
minLength: 3
},
{
name: 'from_email',
displayKey: 'from_email',
source: from_email
});
没用。
首先尝试此操作以检查您的预输入是否真的有效,然后尝试搜索 "Ala" 或 "Cal"。我会根据您的回复更新我的答案。
var emails = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California']
});
$('.typeahead-from-email').typeahead({
hint: true,
highlight: true,
minLength: 3
},
{
name: 'from_email',
source: emails
});
无需调用视图或模板
public function promotion()
{
$this->db->distinct();
$this->db->select("from_email");
$this->db->from('booking');
$query2 = $this->db->get();
$results = $query2->result_array();
echo json_encode($results); //<-- Just echo the json result
}
然后在你的 javascript
prefetch: <?php echo base_url('admin/promotion'); ?>
我在控制器中有一个查询结果,我将其编码为 json
。我需要将它传递给 javascript,因为我想使用 typeahead js
来自动完成搜索输入。这是我的尝试:
我的控制器(Admin.php):
public function promotion()
{
$this->db->distinct();
$this->db->select("from_email");
$this->db->from('booking');
$query2 = $this->db->get();
$results = $query2->result_array();
$data['from_email'] = json_encode($results);
$this->template->render('admin/promotion',$data,'admin');
}
我的输入搜索(根据数据库中的电子邮件地址进行搜索):
<input type="text" name="email" class="form-control input-lg typeahead typeahead-from-email" id="email" required="required" autocomplete="off" tabindex="1" />
Javascript :
var emails = new Bloodhound({
datumTokenizer: function (d) {
return Bloodhound.tokenizers.whitespace('from_email');
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: base_url + 'admin/promotion'
});
$('.typeahead-from-email').typeahead({
hint: true,
highlight: true,
minLength: 3
},
{
name: 'from_email',
displayKey: 'from_email',
source: from_email
});
没用。
首先尝试此操作以检查您的预输入是否真的有效,然后尝试搜索 "Ala" 或 "Cal"。我会根据您的回复更新我的答案。
var emails = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California']
});
$('.typeahead-from-email').typeahead({
hint: true,
highlight: true,
minLength: 3
},
{
name: 'from_email',
source: emails
});
无需调用视图或模板
public function promotion()
{
$this->db->distinct();
$this->db->select("from_email");
$this->db->from('booking');
$query2 = $this->db->get();
$results = $query2->result_array();
echo json_encode($results); //<-- Just echo the json result
}
然后在你的 javascript
prefetch: <?php echo base_url('admin/promotion'); ?>