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

时间:2018-10-05 07:19:15

标签: php jquery ajax codeigniter

我正在处理一个网站(许多旧代码),但发现了一个未知的问题,很难解决。 星期三使用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和表单发出发布请求,结果相同

2 个答案:

答案 0 :(得分:1)

您必须在点击功能末尾添加返回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;
})

答案 1 :(得分:0)

在操作2|3: 010 | 011 = 011 (3 dec)中添加此代码

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" >

});

相关问题