Problems passing POST parameter through Ajax

0

I'm having trouble passing parameters via POST to my PHP methods. I have the JS variable properly populated with the attributes I want to pass. But when trying to access the variable in my PHP code it says it is undefined. I'm using Codeigniter as a framework.

JS Code:

var arr = [];
var vars = arr.concat(put, pedidoData);
$.ajax({
   url: "controller/metodo",
   type: 'POST',
   data: {vars:vars},
   success: function(r) {
     if (r.status == "success"){                                                       
         console.log("Sucesso");
     }else{
         console.log(vars);
         console.log("erro na inserçao");
         console.log(r);
     }
   }
});

Controller.php code:

public function metodo(){
    $resposta = $this->input->post();
    $response = $this->meumodel_model->metodo($resposta);
    if(is_null($response)){
        $this->response(array("status"=>"erro", "erro"=>"Resposta nula ou vazia."), 200);
    }else{
        $this->response($response, 200);
    }
}

meumodel_model.php code:

  public function metodo($vars = array()){

    $variaveis = array();
    $inserir = "SQL utilizando as variaveis...";
    $q = $this->db->query($inserir); 

    if($q){
        return array("status"=>"success");
    }else{
        return array("status"=>"error");
    }
}

I wonder what I'm doing wrong. Thankful.

    
asked by anonymous 11.04.2018 / 14:10

0 answers