2016-09-22 35 views
0

使用jQuery的数据路由我有这样如何通过在Laravel

Route::get('user/{id}/article', '[email protected]')->name('Show'); 

一个途径,并需要使用jQuery

$('.user').click(function() { 
    var id = $(this).attr('data-id'); 
    window.location.href ='{{ route('Show',['id'=> id]) }}'; 
}); 

id传递参数ID不能老是被认可, 我应该怎么做?

+0

你的HTML链接,请给我的html代码 – Komal

回答

0

可以传递值,如下面的代码:

  <script type="text/javascript"> 
      $('#modal-save').on('click',function(){ 
       $.ajax({ 
        type : 'post', 
         url : urlEdit, 
         data : {body : $('#post-body').val(), postId: postId , _token:token } 
       }).done(function(msg){ 
       //console.log(msg.new_body); 
        $(PostBodyElement).text(msg['new_body']); 
        $('#edit-modal').modal('hide'); 
     }); 
    }); 
       </script> 

这应该是在你的控制器 公共职能postEditPost(请求$要求) {$ 这 - >的validate($请求,[ “ body'=>'required' ]); $ post = Post :: find($ request ['postId']); if(Auth :: user()!= $ post-> user) { return redirect() - > back(); } $ post-> body = $ request ['body']; $ post-> update();返回response() - > json(['new_body'=> $ post-> body],200); }

而在乌尔路线: 路线::交( '/编辑',[ '使用'=> 'PostController中@ postEditPost', '作为'=> '编辑' ]);

0

尝试生成这样

<a href="user/{{ $user->id }}/article">{{ $user->name }}</a> 
+0

是它有一个可以调用的名字的路线?不使用网址。 – Fan

+0

是的,但PLZ解释我的网址,你想要的。 – Komal