0
I need to pass parameters in the AJAX URL in order to filter the name of the searched product. I mean, I have a type field text
and when the person type it already shows the product typed, same as the Facebook search.
How do I pass these parameters?
Follows the code:
$("#buscarProduto").keyup(function (e)
{
e.preventDefault();
$('.item-selecionado').fadeOut();
var buscarProduto = $(this).val();
var idcategoria = $(".active-button").attr("id");
var mostraPesquisa = "";
if(buscarProduto.length == 0)
{
$('.item-selecionado').fadeIn();
}
else
{
$.ajax({
url: 'http://api.teste/store/produtos?filter[categoria_id]=' + idcategoria + '?like[nome]=' + buscarProduto + '',
method: 'GET',
dataType: 'json',
success: function(retorno)
{
$.each(retorno.data, function(i, item)
{
alert(item.nome);
// mostraPesquisa += '<li class=\'item-pesquisado\'><span class=\'\'>Nome do Lanche</span></li>';
})
$('.item-selecionado').fadeIn();
$('.item-selecionado').html(mostraPesquisa);
},
error: function(XMLHttpRequest, textStatus, errorThrown)
{
alert("Status do Servidor: " + textStatus);
alert("Erro do Servidor: " + errorThrown);
}
});
}
});
If I pass like this:
url: 'http://api.teste/store/produtos?filter[categoria_id]=' + idcategoria, method: 'GET', dataType: 'json', date: { queryString: "" + searchProduct + "" }
What is the current code problem?
– Woss
When passing: url: 'http://api.test/store/products?filter[categoria_id]=' + idcategoria + '?like[name]=' + buscarProduto + ', it returns nothing
– Felipe Michael da Fonseca
But in the API come the correct values?
– Woss
Yes. Only at the time of doing the research. When I gave that Alert there in the foreach it does not return. I do not know if I’m doing right that like there
– Felipe Michael da Fonseca
What is the result of
console.log(retorno)
within the callbacksuccess
?– Woss
i would pass the parameters in the attribute
data
ajax instead of passing them via GET in the URL, maybe this would have related to the problem.– Paulo Roberto Rosa
By the way, you used
?like[nome]
, using?
to concatenate the parameters, while the correct one is&
. Should be&like[nome]
– Woss
Even so it gives problem, it does not return anything. And I am using the [name] because I use to list the products and work perfectly.
– Felipe Michael da Fonseca