Answer the question
In order to leave comments, you need to log in
In laravel controller doesn't see ajax data via post, what's the problem?
Laravel 5.6 + JQuery
When sending a GET, returns a response with outgoing parameters, when changing the method to POST, Request::all() returns an empty array.
Everything works on OpenServer, but when transferred to a hosting it breaks.
What changed on the hosting:
- redirection from the root to public
- In the controller //use Illuminate\Http\Request; on use Request (on hosting, for some reason, it only worked like that);
- Registered X-CSRF-TOKEN.
- Threw accesses 777 on some folders for the test.
JS
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
<... код не имеющий отношения к вопросу>
$('[js_event=calc]').click(function(event) {
//event.preventDefault();
//options
item = {};
item['width'] = parseInt( $("[js_data=width]").val() );
item['height'] = parseInt( $("[js_data=height]").val() );
item['calc_name'] = $("[js_data=calc_name]").val();
console.log( item['calc_name'] );
console.log( $('meta[name="csrf-token"]').attr('content') );
//item['_token'] = $('meta[name="csrf-token"]').attr('content') ; // в круг решил обмазаться для верности
//item['_method'] = 'POST' ;
$.ajax({
url: '/calculation/json/',
type: 'POST', // c GET работает !!!!!!!!!
data: item,
headers: { 'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content') },
success: function(resp){
console.log( "submit form success" );
console.log( resp );
}
});
event.preventDefault();
});
//use Illuminate\Http\Request;
use Request;
<... код не имеющий отношения к вопросу>
public static function json2() {
return print_r( Request::all(), true );
//return print_r( Request::post(), true );
//return json_encode( Request::all(), JSON_UNESCAPED_UNICODE );
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question