Doubt ajax in data

0

In this code to send data by ajax, how is the data part interpreted? The id does not seem to assign it with the colon (:) why? Also, where does the id come from (if you need it, I'll pass the complete code). I am involved in this part (below I put the way that I usually understand it):

function eliminar (id){
    var q= $("#q").val();
    var id_categoria= $("#id_categoria").val();
    $.ajax({
        type: "GET",
        url: "./ajax/buscar_productos.php",
        data: "id="+id,"q":q+"id_categoria="+id_categoria,
         beforeSend: function(objeto){
            $("#resultados").html("Mensaje: Cargando...");
          },
        success: function(datos){
        $("#resultados").html(datos);
        load(1);
        }
    });
}

Until now I understood well how to pass the data like this:

    function ejecutarAjax(event) {
  event.preventDefault();
    //alert("hola");
  var datosEnviados =
  {
    'usuario' : $('#txtUsuario').val(),
    'contra'  : $('#txtPassword').val()
  };
  $.ajax({
    type      : 'POST',
    url       : 'registrar_usuario.php',
    data      : datosEnviados,
    dataType  : 'json',
    encode    : true,

But this way of passing the data baffles me could someone help me understand it?

    
asked by RicardoKra 09.10.2018 в 12:11
source

2 answers

1

This is an example of the jQuery documentation :

$.ajax({
  method: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" }
})
  .done(function( msg ) {
    alert( "Data Saved: " + msg );
  });

See the creation of variable data :

  data: { name: "John", location: "Boston" }

As you can see, the value of the left in each key / value pair separated by the two points can be written without quotes and will be the key that you will look for in the server with $_POST or with $_GET depending on the case.

As for the value on the right, it can be either a value that you retrieve right there, that you write by hand, or that you collect from a variable.

In your case, you can do it like this:

    data: {id:id, q:q, id_categoria:id_categoria}

As for the doubt you have about id , it is the parameter that the delete function receives, therefore you can use it, because it is within the scope of the function.

    
answered by 09.10.2018 в 12:23
1

Let's extract the configuration object and see what really happens:

const id=0;
const q= 'Q';
const id_categoria='CAT'

let datos = {
  type: "GET",
  url: "./ajax/buscar_productos.php",
  data: "id="+id,"q":q+"id_categoria="+id_categoria,
  beforeSend: 'beforeSend',
  success: 'success'
}

console.log(datos);

As you can see, that code could be formatted / devised as:

function eliminar (id){
    var q= $("#q").val();
    var id_categoria= $("#id_categoria").val();
    $.ajax({
        type: "GET",
        url: "./ajax/buscar_productos.php",
        data: "id="+id,
        "q":q+"id_categoria="+id_categoria, //las comillas son optativas
        beforeSend: function(objeto){
          $("#resultados").html("Mensaje: Cargando...");
        },
        success: function(datos){
          $("#resultados").html(datos);
          load(1);
        }
    });
}

That is, really "q" is an attribute of the configuration object of the AJAX call that is surely being ignored by jquery.

    
answered by 09.10.2018 в 13:25