A Ghosal A Ghosal - 6 months ago 21
Ajax Question

Ajax (in Laravel project) is not working on a page, while working same code on another page

I am new in coding. Having some problem in Ajax code in Laravel 5.2 project.
THE CODE IS RUNNING ON REGISTRATION PAGE, BUT NOT ON ANOTHER PAGE, ON WHICH I NEED TO DO.
Please help me.

The Routes -

Route::post('like-article',['as'=>'like_article','uses'=>'LikeController@likeArticle']);


The controller -

public function likeArticle(){
$like_article_id = trim(Input::get('like_article_id'));
$article_count = DB::table('articles')
->where('id',$like_article_id)
->count();
if($article_count){
return "1";
}
else{
return "0";
}
}


Ajax part -



$("#like_thumb_up_img").click(function() {
//alert('Alert !');
$.ajax({
url : 'like-article',
type:'post',
dataType:'HTML',
data : {like_article_id:$('#like_article_id').attr('value')},
success:function(result){
//alert(result);
if(result=="1")
{
$('#like_show_div').html('Got Article !');
}
else if(result=="0")
{
$('#like_show_div').html('Not Found !');
}
}
})
})

<div class="row">
<input type="hidden" id="like_article_id" value="{{$article[0]->id}}">
<img id="like_thumb_up_img" class="img-responsive custom_margin_auto custom_margin_top_bottom_5" src="{{URL::asset('system_images/thumb_up.png')}}" alt="Thumb up">
</div>
<div id="like_show_div" class="row">
</div>





Thank you in advance for finding the bug.

Answer

Change your url using the url() or secure_url()(if you have a ssl certificate) function

$("#like_thumb_up_img").click(function(e) {
 e.preventDefault();
 var like_article = $('#like_article_id').attr('value');
$.ajax({
    url : "{{url('like-article')}}",
    type:'post',
    dataType:'HTML',
    data : {like_article_id:like_article},
    success:function(result){
        //alert(result);
        if(result=="1")
        {
            $('#like_show_div').html('Got Article !');
        }
        else if(result=="0")
        {
            $('#like_show_div').html('Not Found !');
        }
         }
       })
      })
Comments