Support the ongoing development of Laravel.io →
Requests Input

I am trying to make a POST request within a js function.

It is sending an array of data that is stringified. My GET request is working fine.

here is what I have. In app.blade.php I have this in the head <meta name="_token" content="{!! csrf_token() !!}"/> and this at the bottom after my jquery script

<script type="text/javascript">
  $.ajaxSetup({
  headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') }
  });
</script>

Then function I am working with looks like this:

function saveRegions() {
    var data = JSON.stringify(
        Object.keys(wavesurfer.regions.list).map(function (id) {
            var region = wavesurfer.regions.list[id];
            return {
                start: region.start,
                end: region.end,
                data: region.data
            };
        })
    );
    $.ajax({
      url: '../annotations/regions.json',
      type: "post",
      data: data,
      success: function(data){
        alert(data);
      }
    });
};

This function fires on a DOM event and is done many times on the page without any reload. The data isn't manually inputted by the user so I can't just do a form.

I am just trying to send it to a json file within my public directory right now.

I have added this route: Route::post('wave', 'WaveController@send'); and this function to go with

public function send ()
  {
    // Getting all post data
    if(Request::ajax()) {
      $data = Input::all();
      print_r($data);die;
    }
  }

The laravel code is pieced together from suggestions I've read about. I'm very new to php and laravel and used to working on the client side in js only.

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

maxehnert maxehnert Joined 28 Apr 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.