1

我在控制器中接收到一个变量,我想用搜索变量进行绑定查询,我试试这个:

$search = $this->request->getPost('term');

                    $item = Item::find(
                            [
                                    'columns' => 'name',
                                    'conditions' => "name LIKE :searchT: ",
                                    'bind' => [
                                            'searchT' => '%'.$search.'%',  
                                    ],
                            ]
                            );

上面的代码返回了与 LIKE 限制不匹配的项目。

如果我通过字符串字面工作正常:

$item = Item::find(
                            [
                                    'conditions' => "name LIKE '%Os%' ",
                                    'columns' => 'name',
                                    'limit' => 10,
                            ]
                            );

查询:

<script type="text/javascript">
$( function() {
    $("#itemSearch").autocomplete({
        source: function ( request, response ) {
            $.ajax({
                type: "POST",
                url: "/item/search",
                dataType: "json",
                data: { 
                    term: request.term    
                },
                contentType: "application/json",
                success: function(data) {
                    response(data);
                }
            });
        },
        minLength: 2
    })
})
</script>
4

1 回答 1

1

我的 $search 是空的,在我的 JQuery 中我需要在 ajax 中添加一个标题:

...
source: function ( request, response ) {
            $.ajax({
                type: "POST",
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },  
...

谢谢@Abhik Chakraborty

于 2017-09-23T23:48:01.197 回答