Monday, March 6, 2017

Handling Ajax requests (success, errors messageS)

normally i dont work much with ajax and laravel, but in my laravel app i need to make some ajax requests, and also handling errors and feedback. I see many material but im looking the right and best way in hadling from the back-end to the front-end (php and js).

Here is my code js:

$(function() {

            $('.send').click(function(event){

                event.preventDefault();

                $.ajax({
                    url: "",
                    type: "post",
                    data: {'name':$('#name').val(), 'surname':$('#surname').val(), '_token': $('input[name=_token]').val()},
                    success: function(data){
                       console.log("sucess");
                    }
                });
            });

        });
    </script>

Controller:

public function storeAjax(Request $request)
    {

        $validator = Validator::make($request->all(), [
            'name'             => 'required',
            'surname'          => 'required'
        ]);
        if ($validator->fails()) {
            return response()->json($validator->messages(), 200);
        }

    }

But is giving me 500 error, so hope someone can guide me in the best way in handling requests between laravel and ajax.



via Pedro

Advertisement