【问题标题】:How to pass the data to route using jquery in Laravel如何在 Laravel 中使用 jquery 将数据传递给路由
【发布时间】:2017-01-30 22:12:27
【问题描述】:

我有这样的路线

Route::get('user/{id}/article', 'ArticleController@show')->name('Show');

并且需要使用jquery传递参数id

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

但是id无法识别,怎么办?

【问题讨论】:

  • 请给我html代码

标签: php jquery laravel routes


【解决方案1】:

尝试像这样生成您的 html 链接

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

【讨论】:

  • 是否可以通过名称调用路线?不使用网址。
  • 是的,但是请解释一下你想要的网址。
【解决方案2】:

您可以像下面的代码一样传递值:

<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');
        });
    });

这应该在你的控制器中

public function postEditPost(Request $request)
                    {
                     $this->validate($request,[
                      'body' => 'required'
                        ]);
                       $post = Post::find($request['postId']);
                     if(Auth::user() != $post->user)
                         {
                         return redirect()->back();
                        }
                     $post->body = $request['body'];
                   $post->update();
                         return response()->json(['new_body' => $post->body],200);
                }

在你的路线上:

 Route::post('/edit',[
              'uses' => 'PostController@postEditPost',
              'as' => 'edit'
                  ]);

【讨论】:

    【解决方案3】:
    $('.user').click(function() {
        var id = $(this).attr('data-id');
        var url = '{{ route("Show", ":id") }}';
        url = url.replace(':id', id);
        window.location.href = url;
    });
    

    【讨论】:

      猜你喜欢
      • 2015-10-15
      • 2018-06-20
      • 2015-03-16
      • 1970-01-01
      • 2016-07-01
      • 2019-03-03
      • 2018-08-14
      • 2022-01-13
      • 2020-01-23
      相关资源
      最近更新 更多