Doubt with JSON in Autocomplete JQuery

1

I have the following JSON return

{"cliente":[  
            {"id":"1","nome":"Fulano"},  
            {"id":"2","nome":"Ciclado"}  
]}

And I need my popular autocomplete ,

$('#nome').autocomplete({  
      source: function(request, response){  
        $.ajax({  
            url:"http://192.168.254.103:12514/web-parbs/htdocs/json/clientes.jsp",  
            dataType:"json",  
            success: function(data){  
                response($.map(data, function(item){  
                    return {  
                        label: item.id,  
                        value: item.nome  
                    };  
                }))  
            }})  
      }  
    });

This was one of the solutions I researched, the problem is that the item returns a Vector, I'm having trouble going in a way that stays label: the name of the client to be displayed in the field and the value: the id of the client. Can someone help me?

    
asked by anonymous 23.11.2014 / 17:14

1 answer

1

The map function translates an object (or list) into a list, where you must pass a function that does the transformation. It iterates over the properties and invokes its function to populate the new list.

The problem in your case is that you are passing an object with only one property of your own instead of passing the list of clients.

Just call the $.map function by passing data.cliente instead of data only.

This JSFiddle exemplifies the solution.

    
23.11.2014 / 18:05