0

I'm trying to add form data to a dB using javascript. The postData is being read I can see in an 'Alert' but the URL isn't executing. I've put it in as 'donorlist' and '/donorlist'.

$(document).ready(function() {

$('#add-donor-form').submit(function(evt) {
  /* Act on the event */
  evt.preventDefault();

  var postData = $(this).serialize();


  $.post('/donorlist', postData, function(donor_data) {
    /*optional stuff to do after success */
    // $("#show-donor").html(donor_data);
  });

});

});

The form tag is:

  {!! Form::open(['method'=>"post", 'class' =>'form-horizontal', 'id' =>"add-donor-form"]) !!}

Route:

Route::post('/donorlist','AjaxController@addonor');
0

2 Answers 2

1

Hope I understood your query correctly. You have to wrap quotes around your URI in the post statement:

$.post('/donorlist', postData, function(donor_data) {
    /*optional stuff to do after success */
});

See Fiddle

Sign up to request clarification or add additional context in comments.

6 Comments

What does "php artisan route:list" say?
Route::post('/donorlist','AjaxController@addonor');
POST | donorlist | | App\Http\Controllers\AjaxController@addonor
If you change the route to GET, can you access it in the browser?
no, still nothing. I still can get an alert with the postData
|
0

The problem was not haveing the csrf token in js.

$(document).ready(function() {

$('#add-donor-form').submit(function(evt) {
  /* Act on the event */
  evt.preventDefault();

  var postData = $(this).serialize();
  var url = $(this).attr('action');

  $.ajaxSetup({
      headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
      }
    });

  $.post(url, postData, function(donor_data) {
    /*optional stuff to do after success */
    $("#show-donor").html(donor_data);
  });

});

});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.