I have a case where i'm calling ajax post to a controller and save data. I have tried in my development server it is working fine, but when i deploy to production, it is always redirecting
this is my js file
function ajaxPost(Url,FormData){
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
'Accept':'application/json'
}
})
return $.ajax({
url : Url,
type : "POST",
data : FormData,
dataType : "json",
});
}
var FormData = {
uuid : localStorage.getItem('uuid'),
q_id : $select.data('question-id'),
a_id : $(this).data('answer')
};
localStorage.setItem($select.data('question-id'), $(this).data('answer'));
ajaxPost('ajax/save', FormData).done(function(){
setTimeout(function(){
location.reload();
$select.removeClass('question__slide-in question__slide-in-reverse').addClass('question__slide-out');
setTimeout(function(){
$select.attr('hidden', 'hidden');
},450);
}, 300);
});
this is the HomeController
public function save(Request $request)
{
$data = $request->toArray();
if(is_array($data['a_id'])){
foreach($data['a_id'] as $value){
$data = new UserAnswer();
$data->user_id = $request->uuid;
$data->question_id = $request->q_id;
$data->answer_id = $value;
if($request->has('custom'))
$data->custom_value = $request->custom;
$data->save();
}
}else{
$data = new UserAnswer();
$data->user_id = $request->uuid;
$data->question_id = $request->q_id;
$data->answer_id = $request->a_id;
if($request->has('custom'))
$data->custom_value = $request->custom;
$data->save();
}
return response()->json();
}
web routes
Route::get('/', 'HomeController@index');
Route::post('ajax/save', 'HomeController@save');
it is working in my dev as we can see in the image below. return 200 and the response header is application/json
development
but in the production (which im using AWS apache/2) getting 302 and response header is text/html
production
Appreciate all answer. Hopes it will solve my problem
Thank You
question from:
https://stackoverflow.com/questions/66057385/laravel-jquery-ajax-post-controller-always-redirect-302 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…