Pass Ajax URL parameters

Asked

Viewed 1,709 times

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 + "" }

returns an empty array. See image: inserir a descrição da imagem aqui

  • 1

    What is the current code problem?

  • When passing: url: 'http://api.test/store/products?filter[categoria_id]=' + idcategoria + '?like[name]=' + buscarProduto + ', it returns nothing

  • But in the API come the correct values?

  • 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

  • What is the result of console.log(retorno) within the callback success?

  • 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.

  • By the way, you used ?like[nome], using ? to concatenate the parameters, while the correct one is &. Should be &like[nome]

  • 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.

Show 3 more comments

1 answer

1

It’s been so long, but I use the parameter pass via date field: within the $.ajax tag.

Your example:

$.ajax({
        url: 'http://api.teste/store/produtos'
        type: 'GET',
        dataType: 'json',
        data: {idcategoria  : idcategoria  , buscarProduto : buscarProduto }
        success: function(retorno)
        {
          ...
         }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.