Question

I got a simple question, I can't make the code to output only one result instead of multiple, i want to get only one first result, but it makes multiple because of each

looked up for stringify but couldn't manage to make it work properly

$('#form').submit(function(){
    var nick=$('#username').val();
    if(nick.length > 0){ 
        var url='http://api.worldoftanks.ru/2.0/account/list/?application_id=171745d21f7f98fd8878771da1000a31&search='+nick;
        $.getJSON(url, function(json) {
            $('#result').html('');
            $.each(json.data, function(index, data) {
                var id=data.id;
                $('#result').append('<p> ID: ' + id+ '</p>');
            });
        });
    }else{ 
        alert('enter username');
    }
});
Était-ce utile?

La solution

No need of each you can get first id using

json.data[0].id

Autres conseils

You could also include "return;" after
appending the json data so that loop will return after the first loop, otherwise it's better to get data using json index.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top