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.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community