简体   繁体   English

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

[英]POST requests are redirected to a GET resquest

I'm dealing with a website(lots of legacy code ) and I've found a problem that I don't know hot to solve. 我正在与一个网站(很多旧代码)打交道,但发现一个我不知道要解决的问题。 The wed uses codeigniter controllers and twig templates,the problem is that when it makes a post request it is redirected to a get request to the same url(losing the data), any idea about how could this redirection it's done? 星期三使用Codeigniter控制器和Twig模板,问题在于,当它发出发布请求时,将其重定向到对相同URL的get请求(丢失数据),关于如何进行重定向的任何想法?

This is how the request it's done,(same problem with html form) 这是请求的完成方式(与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);
    });
}); 

this is the form 这是表格

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

I've tried to make the post request with ajax and a form ,with the same result 我试图用ajax和表单发出发布请求,结果相同

You must add a return false; 您必须添加return false; at the end of the click function like this : 在单击功能结束时,如下所示:

    $('#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;
})

Add this code in from action javascript:void() 在动作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