I have created a like and dislike a button and store the info through ajax in Laravel 5.2. I am using wamp as my localhost. At first, I saw sometimes the like and dislike was counted, and sometimes they weren't. So, I tried to see it in the console through console.log(), and found 500(internal Server Error) in some of my clicks. I also made sure the csrf token is provided properly.
I don't know how to deal with an error which sometimes come and sometimes doesn't.
this is my likeajax.js :
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
var postId=0;
$('.option1').on('click', function (event) {
event.preventDefault();
$('event').attr('disabled', 'disabled');
postId = event.target.parentNode.parentNode.parentNode.parentNode.dataset['postid'];
$.ajax({
method: 'GET',
url: urlLike,
data: {postId: postId, _token: token},
})
});
$('.optionx').on('click', function (event) {
event.preventDefault();
$('event').attr('disabled', 'disabled');
postId = event.target.parentNode.parentNode.parentNode.parentNode.dataset['postid'];
$.ajax({
method: 'GET',
url: urlDislike,
data: {postId: postId, _token: token},
})
});
this is my LikeController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Depress;
use App\Http\Requests;
class LikeController extends Controller
{
public function postLike(Request $request)
{
$post_id = $request['postId'];
$post = Depress::find($post_id);
$post->like = $post->like+1;
$post->save();
return null;
}
public function postDislike(Request $request)
{
$post_id = $request['postId'];
$post = Depress::find($post_id);
$post->dislike = $post->dislike+1;
$post->save();
return null;
}
}
this is in my like layout.blade.php,
<script>
var token = "{{ csrf_token() }}";
var urlLike = '{{ route('like') }}';
var urlDislike = '{{ route('dislike') }}';
</script>
Any help will be really appreciated.
Hello, I had the same problem with the same setup as you but I resolved this switching from WAMP (To Nginx with PHP-fastCGI 7.05) and the problem was resolved (At this time at least), ive tested both and the nginx php-fastcgi 7-05 solution havent failed me yet.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community