簡體   English   中英

POST請求重定向到GET重新請求

[英]POST requests are redirected to a GET resquest

我正在與一個網站(很多舊代碼)打交道,但發現一個我不知道要解決的問題。 星期三使用Codeigniter控制器和Twig模板,問題在於,當它發出發布請求時,將其重定向到對相同URL的get請求(丟失數據),關於如何進行重定向的任何想法?

這是請求的完成方式(與HTML表單存在相同的問題)

$('#send_form').click(function(){
    var selected =$('#profile_select').val();
    $.ajax({
        url:"{{ base_url }}myurl/encuestas/perfil_interna",
        method:"post",
        data:{
            selected:selected
        }
    }).done(function(response){
        console.log(response);
    }).fail(function(err){
        console.log(err);
    });
}); 

這是表格

<form action="{{ base_url }}myurl/encuestas/perfil_interna">
    <select multiple name="profile_questions[]" id="profile_select">
          {% for question in questions %}
              <option value="{{question.id}}" id="qid_{{question.id}}">{{ question.text }}</option>
          {% endfor %}
     </select>
     <input type="hidden" name="{{csrf.name}}" value="{{csrf.hash}}"/>
     <input type="submit" value="Enviar" id="send_form" >
 </form>

我試圖用ajax和表單發出發布請求,結果相同

您必須添加return false; 在單擊功能結束時,如下所示:

    $('#send_form').click(function(){
    var selected =$('#profile_select').val();
    $.ajax({
        url:"{{ base_url }}myurl/encuestas/perfil_interna",
        method:"post",
        data:{
            selected:selected
        }
    }).done(function(response){
        console.log(response);
    }).fail(function(err){
        console.log(err);
    });
  return false;
})

在動作javascript:void()添加此代碼

<form action="javascript:void()">
<select multiple name="profile_questions[]" id="profile_select">
      {% for question in questions %}
          <option value="{{question.id}}" id="qid_{{question.id}}">{{ question.text }}</option>
      {% endfor %}
 </select>
 <input type="hidden" name="{{csrf.name}}" value="{{csrf.hash}}" id="token"/>
 <input type="submit" value="Enviar" id="send_form" >

$('#send_form').click(function(){
var selected =$('#profile_select').val();
var token=$('#token').val();
$.ajax({
    url:"{{ base_url }}myurl/encuestas/perfil_interna",
    method:"post",
    data:{
        selected:selected,
        _token:token
    }
}).done(function(response){
    console.log(response);
}).fail(function(err){
    console.log(err);
});

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM