1
votes

I am building an application where users can upload projects. I am implementing a system where users can 'Like/Unlike' other projects. I am trying to use an AJAX call to save likes. Users are able to like projects on the detail page of a project (/projects/{id})

I have a table users, projects and likes. My plan is to save the likes in the likes table obviously so a record looks like this: id, user_id, project_id. In the future I can do a COUNT query and find out how many likes each project has, etc.

For some reason when I click on the like button my application gets stuck and keeps loading until it crashes.

My files: Ajax call

$(document).ready(function(){
        console.log("js loaded");
        $('#likeform').on('submit', function(e) {
            console.log("like submitted");
            e.preventDefault();

            $.ajax({
                url: $(this).parent().attr('action'),
                type: "post",
                data: {'_token': token, 'user_id': $('input[name=user_id]').val(), 'project_id': $('input[name=project_id]').val()},
                success: function(data){
                    alert(data);
                }
            })
        });
    });

My Form

  {!! Form::open(array('url'=>'projects/'.$project->id.'/like','method'=>'POST', 'id'=>'likeform')) !!}
    <input type="hidden" id="token" value="{{ csrf_token() }}">
    {!! Form::Submit('Like', array('class'=>'send-btn')) !!}
{!! Form::close() !!}

My Routes:

Route::get('/', 'LikeController@index');
Route::post('projects/{id}/like', 'LikeController@like');

My like function in the LikeController:

public function like()
{
    if(Request::ajax()) {
        $data = Input::all();
        print_r($data);die;
    }
}
1
Does the browser console say something while loading? Did you check your laravel.log? Provide any kind of error will greatly improve your chances to get a proper answer.MisterP
There's nothing in my console. And I get no errors at all which is kinda annoying.Moussa Chaabar
Weird. Anyway, I did not test your code, but you're trying to retrive an action attribute from your form while it doesn't have one. You did set an url instead. I'm not confident this is what you're looking for, but it's worth a try imo. Side note: building a form with the Form facade will automatically set an hidden token field, so there's no actually need to add to your form a second one.MisterP
Oh okay I was wondering why there are 2 hidden token fields. Now I know :)Moussa Chaabar
action vs url was a nonsense. My bad. Still is quite strange you are not getting any error, at least within the server log.MisterP

1 Answers

0
votes

The

gets stuck and keeps loading until it crashes

part may be related to the fact that the ajax post expects some kind of response, but you are not responding at all in your controller. Try something like

public function like()
{
    if(Request::ajax()) {
        $data = Input::all();
        return Response::json([
                'error' => false,
                'insertedData' => $data
            ], 200);
    }
}  

Remenber to use Response in your controller. Now you are returning something to the ajax call and you should be able to access $data

success: function(data){
            alert(data.insertedData);
         }

Are you experiencing the same behaviour? If not you can move on and perform the actual db insert.