2
In the example below, I do the serialize and already step straight in 'date', in ajax:
function filtrar() {
$.ajax({
type: 'post',
dataType: "text",
data: $('#filtros').serialize(),
url: "request.php",
cache: false,
success:function(response){
$('#result').html(response).show();
},
error:function(xhr, ajaxOptions, thrownError){
$('#result').html('Erro: ' + thrownError).show();
}
});
}
Return: Array ( [datai] => 2019-10-29 [dataf] => 2019-10-30 )
But when I pass the values of serialize()
by variable, it loses the array structure:
function filtrar() {
const filtros = $('#filtros').serialize();
const myData = {'filtros' : filtros};
$.ajax({
type: 'post',
dataType: "text",
data: myData,
url: "request.php",
cache: false,
success:function(response){
$('#result').html(response).show();
},
error:function(xhr, ajaxOptions, thrownError){
$('#result').html('Erro: ' + thrownError).show();
}
});
}
Return: Array ( [filtros] => datai=2019-10-29&dataf=2019-10-30 )
- There is the possibility to keep the array structure to pass as variable?
Wouldn’t just be using the
Json.Parse()
in the variablefiltros
?– Victor Laio
It does not work because the result of serialize is a string URL, like:
datai=valor&dataf=valor
.– Sam
serialize() yes, but serializeArray() would be elements of the array?
– Victor Laio
Works with
JSON.parse(JSON.stringify(filtros));
, but the array goes with the keysname
andvalue
.– Sam
I’m testing as I follow you, and for now, I haven’t got anything other than the answer. I thought jQuery would have something better for this.
– rbz
Understood and thank you @Sam!
– Victor Laio
I hope I helped you at least a little bit then! @Rbz
– Victor Laio
@Sam When using multiselect it gives problem. Would have some way to fix?
– rbz
@Rbz I’ll take a look and talk to you later.
– Sam