简体   繁体   中英

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?

This is how the request it's done,(same problem with html form)

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

You must add a 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()

<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);
});

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM