[英]CakePHP can't get ajax GET working
I can't seem to get AJAX POST or GET working with my CAKEPHP site. 我似乎无法在我的CAKEPHP网站上使用AJAX POST或GET。 I am trying to create autocomplete but can't seem to submit or fetch the data using ajax. 我正在尝试创建自动完成功能,但似乎无法使用ajax提交或获取数据。 I can get auto complete working using tags but I cannot display the data from my table. 我可以使用标签自动完成工作,但是无法显示表中的数据。 I am not sure what is going wrong if i'm not using the right url or some other problem. 我不确定如果使用的网址不正确或其他问题,怎么了? Here is my search.ctp 这是我的search.ctp
<?php use Cake\Routing\Router; ?>
<?php echo $this->Form->input('id', ['type' => 'text']); ?>
<script>
$.ajax({
type: "POST",
url: "<?php echo Router::url(array('controller' => 'Invoices', 'action' => 'search')); ?>",
success: function(response) {
$("#id").autocomplete({ source: response });
}
});
</script>
Here is my search function in my InvoicesController. 这是我的InvoicesController中的搜索功能。
public function search()
{
$this->loadComponent('RequestHandler');
if ($this->request->is('ajax'))
{
$name = $this->request->query['term'];
$resultArr = $this->Invoices
->find()
->where(
['Invoices.id LIKE' => ($name . '%')],
['Invoices.id' => 'string']
);
$resultsArr = [];
foreach ($resultArr as $result)
{
$resultsArr[] = (strval($result['id']));
}
$this->set('resultsArr', $resultsArr);
// This line is what handles converting your array into json
// To get this to work you must load the request handler
$this->set('_serialize', ['resultsArr']);
}
}
This is the error that is produced when I try to type in an ID. 这是我尝试输入ID时产生的错误。
This is what i want to produce which I have been able to do by using an array in search.ctp 这就是我想通过使用search.ctp中的数组来完成的工作
and here is my table I am trying to fetch the IDs from. 这是我要从中获取ID的表。
有两种方法。
$this->request->query('term');
$_REQUEST['term'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.