php - Laravel - load post with custom id via ajax -


i want load post via ajax when click on post link, how can send post id via ajax?

i've got link in blade file:

{{ link_to_route($post->type, $post->comments->count() . ' ' . t('comments'), array('id' => $post->id, 'slug' => $post->slug), array('class' => 'link--to--post')) }} 

i've got following route definition:

route::get('news/{id}-{slug?}', ['as' => 'news', 'uses' => 'newscontroller@show']); 

and controller action:

public function show($id, $slug = null) {     $post = $this->news->getbyid($id, 'news');      if ($slug != $post->slug){      return redirect::route('news', ['id' => $post->id, 'slug' => $post->slug]);     }      event::fire('posts.views', $post);      return view::make('post/post', compact('post')); } 

and i'm trying this:

var posttitle = $(".link--to--post");  posttitle.click(function () {        $.ajax({         url: '/news/{id}-{slug}',         type: 'get',         data: $(this),         success: function (data) {             console.log(data)         }           });      return false; }); 

but doesn't work me, doing wrong?

your javascript should pass parameters this:

$.ajax({     url: '/news/' + id + '-' + slug,     success: function (data) {         console.log(data)     }       }); 

you had url: '/news/{id}-{slug}', perform request /news/{id}-{slug}, isn't valid route -- want url /news/1-foo.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -