Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
495 views
in Technique[技术] by (71.8m points)

Laravel jquery ajax post controller always redirect (302)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...