$ this-request-getData (); does not recover the data

0

I'm going crazy with the AJAX response I want to pass a variable by ajax and retrieve it with cakephp.

The variable to recover is " option "

jQuery + AJAX:

$(document).on("click", "#modificar-foto-cuenta", function() {          
                var opcion = $(this).attr("id").valueOf();              
                    $.ajax({
                        type: "POST",
                        url: "/Users/home/",
                        data: "opcion = " + opcion,
                        dataType: 'html',
                        success: function (response) { console.log(response); },
                    error: function () { alert('error'); }
                    });                 
                $('.modalHome').show();         
        });

Driver:

public function home() {
       if($this->request->is('post')) {         
         $data = $this->request->getData();  
         $this->set([
                     'data' => $data,
                     '_serialize' => 'data'
                   ]);        
       }

Vista:

if(isset($data->opcion)) {
    $opcion = $data->opcion;
  }

Well, there is no way he says he does not recognize data and he is so pancho.

I know that the problem is in the controller, because the ajax returns success, so Ajax processes the post well, but for some reason that I do not know the controller does not detect the post, I tried to change post to ajax with the same result.

What am I doing wrong?

EDIT:

I made some changes:

jQuery + AJAX:

$(document).on("click", "#modificar-foto-cuenta", function() {
            var opcion = $(this).attr("id").valueOf();
            $.ajax({
                type: "POST",
                url: "/Users/home/",
                data: { opcion: opcion },
                dataType: 'html',
                success: function (data) { console.log(data); },
                error: function (data) { console.log(data); alert('error no ajax'); }
            });
            $('.modalHome').show();
        });

Driver:

public function home() {
       if($this->request->is('post')) {
         $data = $this->request->getData();         
         $this->set('data', $data);
       }
     }

Vista:

<div class="modalHome">
<?php
     $paso = $data['opcion'];
     echo $this->element('modalHome', ['opcion' => $paso ]);
?>

modalHome

<?php
     echo $opcion;
?>

I get the following error:

  

Notice (8): Undefined variable: data [APP / Template / Users / home.ctp,   line 50]

Line 50 is --- > $paso = $data['opcion'];

But the strangest thing of all is that the console.log (data); if you return the value to me:

<div class="modalHome">
modificar-foto-cuenta</div>
    
asked by Isaac Palacio 28.12.2018 в 21:18
source

2 answers

1

Sola changes your line of data code in your Ajax , you are sending it wrong, leave it as follows:

....
data: {opcion: opcion},
....

The JSON syntax in Javascript tells us to use the format:

{name:"Jhon"}

Where "name" is the name of your variable and "Jhon" the content of this one.

You can see more here .

    
answered by 30.12.2018 / 06:41
source
0

Change your Ajax request for this.

  let url = "https://jsonplaceholder.typicode.com/todos/1";
  
  axios.get(url).then(
    res=>{
       $('#p').html(JSON.stringify(res.data));
    },
    err => {
      $('#p').html(err);
    }

  );
  
  /* No encontre una pagina que me dejara hacer 
     directamente peticiones post pero te dejo 
     la sintaxis
  
  
  axios.post(
  url,
  {
    // parametros
    opcion: $('#input').val(),
    otro: "este es un ejeplo"
  }
  ).then(
    res=>{
       $('#p').html(JSON.stringify(res.data));
    },
    err => {
      $('#p').html(err);
    }

  );
*/
  
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.js"></script>

<input type="hidden" name="var" value="1" id="input"/>

<div id="p">a</div>
    
answered by 30.12.2018 в 06:29