I am looking for a way to execute a second query ajax
with jQuery
data retrieved from the first query, for example:
I enter the number of the document that I want to consult and when click
clicking on the button, it shows the color saved in the database for that user.
$(".botonConsulta").click(function(){
//Capturo el número de documento de un input
var documento = $('.documento');
var datos = new FormData();
datos.append('documento', documento);
$.ajax({
url: 'ruta/archivo.php',
method: 'POST',
data: datos,
dataType: 'json',
success: function(respuesta){
//Muestro en pantalla el color guardado para ese usuario
$('.colorSeleccionado').html(respuesta['color']);
}
})
}
This query returns, let's say, the color "Green"... Now, with that color, I need to create the second query to fetch all the Greenajax
color data from the colors table .
I tried with promise
but it shows false
in console:
$(".botonConsulta").click(function(){
//Capturo el número de documento de un input
var documento = $('.documento');
var datos = new FormData();
datos.append('documento', documento);
var promesa = $.ajax({
url: 'ruta/archivo.php',
method: 'POST',
data: datos,
dataType: 'json',
success: function(respuesta){
//Muestro en pantalla el color guardado para ese usuario
$('.colorSeleccionado').html(respuesta['color']);
}
})
$promesa.done(function(respuesta){
var color= respuesta['color'];
console.log(color) //Muestra en consola "Verde"
var info = new FormData();
info.append('color', color);
$.ajax({
url: 'ruta/archivo.php',
method: 'POST',
data: info,
dataType: 'json',
success: function(respuesta){
console.log(respuesta) //Muestra en consola "False"
}
})
});
})
It should be noted that I still do not fully understand the use of promise
, I do not know if I am implementing it correctly.
How can I execute the second ajax
one so that it shows the values of the colors table with the color returned in the first query?
so it would be with promise
this would be the dependent request
and you join them like this
In this case I would use a function which receives a parameter and then using ajax brings the information, that is:
We follow your click event process, which upon receiving the response, is delivered to the function to process the ajax action and enter the corresponding data
statement
new Promise()
and you need to addpromise.resolve()
.using
then()
instead of function after resolving promiseother ways to do it:
using callbacks
with the fect api . where would it
then
beluego
cleaner than using promises or callbacksusing async and await .
now an example with everything: