更改路线时,Laravel ajax 搜索不起作用 [英] Laravel ajax search not working when I change the route

查看:48
本文介绍了更改路线时,Laravel ajax 搜索不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在使用 ajax 搜索时遇到问题,我认为这与路线有关,但我不确定出了什么问题.也许现在编码还为时过早.

I am currently facing issues with an ajax search, I think it has to do with the routes, but I'm not sure what is wrong. Maybe it's too early for coding.

这有效

<script type="text/javascript">
$('#name').on('keyup',function(){
    $value=$(this).val();
    $.ajax({
        type : 'get',
        url : '{{ URL::to("/search") }}',
        data:{'name':$value},
        success:function(data){
            $('tbody').html(data);
        }
    });
})
</script>

这不起作用

<script type="text/javascript">
$('#name').on('keyup',function(){
    $value=$(this).val();
    $.ajax({
        type : 'get',
        url : '{{ URL::to("/associates/search") }}', // Does not change if route name is used
        data:{'name':$value},
        success:function(data){
            $('tbody').html(data);
        }
    });
})
</script>

路线如下

Route::resource('associates', 'AssociateController');    

Route::get('/search','AssociateController@search');

Route::get('/associates/search', 'AssociateController@search')->name('associate.search');

控制器中的搜索方法

public function search(Request $request)
{   
    if($request->ajax())
    {
        $output="";
        $associates=DB::table('associates')->where('name','LIKE','%'.$request->name.'%')->limit(10)->get();
        if($associates)
        {
            foreach ($associates as $key => $associate) {
                $output.='<tr>'.
                '<td>'.$associate->id.'</td>'.
                '<td>'.$associate->name.'</td>'.
                '</tr>';
            }
            return Response($output);
        }
    }
}

推荐答案

路由 URI /associates/searchresource URI 冲突.

The route URI /associates/search conflict with resource URI.

为什么?

因为您创建了一个带有 URI associatesresource 路由:

Because you created a resource route with URI associates :

Route::resource('associates', 'AssociateController');

此资源保留给:

Verb          Path                             Action  Route Name
GET           /associates                      index   associates.index
GET           /associates/create               create  associates.create
POST          /associates                      store   associates.store
GET           /associates/{associates}         show    associates.show
GET           /associates/{associates}/edit    edit    associates.edit
PUT|PATCH     /associates/{associates}         update  associates.update
DELETE        /associates/{associates}         destroy associates.destroy

这就是 /associates/search 与该资源 URI 冲突的原因.

That's why /associates/search conflict with that resource URI.

如何解决?

您需要,只需单独添加到该方法的路由,在注册资源之前:

You need , just add a route to that method separately, before you register the resource:

Route::get('/associates/search', 'AssociateController@search')->name('associate.search');
Route::resource('associates', 'AssociateController'); 

这篇关于更改路线时,Laravel ajax 搜索不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆