dimanche 17 avril 2016

Laravel 5.2, submit form without refreshing

First I want to say I have tried googling this and have had no success in any suggestions maybe I am just not searching the right thing but... I am trying to submit a form and than have it not have refresh the page after. Just to test this I am doing this:

Controller Method:

public function send_rsvp() {
  return response()->json(['msg' => 'Success!']);
}

Ajax:

$('rsvp').submit(function( event ) {
event.preventDefault();
$.ajax({
    url: '{{ route("send_rsvp") }}',
    type: 'post',
    data: $('rsvp').serialize(),
    dataType: 'json',
    success: function(data){
        alert(data.msg);
    },
    error: function( data ){
        alert(data.msg);
    }
});

});

What happens is it goes to /send_rsvp and shows the text: {"msg":"Success!"}

What I am expecting to happen is that it just should pop up with an alert which would say "Success!"

Anyone have any idea what I am doing wrong?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire