T
T
TchernyavskyDaniil2017-09-14 12:51:46
JavaScript
TchernyavskyDaniil, 2017-09-14 12:51:46

How to display title City instead of ID in VK API?

Good afternoon. Please tell me how to get not the City ID, but the Title? Do not consider it a stupid question, just with the API (that and with JS) that's it - that's where I started to get acquainted.

$('#load').on('click', loadFriends);
    $('#send-message').on('click', sendMessage);
    $(document).on('click', '.open-detail', function(event) {
        event.preventDefault();
        var uid = +$(event.target).attr('data-uid');
        sendRequest('users.get',{user_ids: uid, fields: 'sex,bdate,city,status,online,timezone,photo_big'},
            drawDetailedFriend);
    });


            function getUrl(method, params) {
        // HELPER
        if (!method) throw new Error('Вы не указали метод'); 
        params = params || {};
        params['access_token'] = '023c70ef2042daa5e7ef9e55242154a55e772eecb11dc032f067598c5e1lalala41c0lalala3309c63';
        return 'https://api.vk.com/method/' + method + '?' + $.param(params); 
    }

    function sendRequest(method, params, func) {
        $.ajax({
            url: getUrl(method, params),
            method: 'GET',
            dataType: 'JSONP',
            success: func
        });
    }

    function loadFriends() {
        sendRequest('friends.search', {count: 60, fields: 'photo_100, online'}, function (data) {
            drawFriends(data.response);
        });
    }   

    function drawFriends(friends) {
        var html = '';

        for (var i = 1; i < friends.length; i++) {
            var f = friends[i];
            var online = f.online ? 'Online' : 'Offline';

            html+= 
                '<li>'+
                '<a href="#">'
                +'<img src="'+f.photo_100+'" />'
                    +'<div>'
                        +'<h4>' + f.first_name + ' ' + f.last_name + '</h4>'
                        +'<p>'+ online + '</p>'
                        +'<button data-uid="'+f.uid+'" class="open-detail">Открыть</button>'
                    +'</div>'
                +'</a>'
                +'</li>';
        }

        $('ul').html(html);
    }   

    function drawDetailedFriend(data) {
        var user = data.response[0];
        var $detail = $('.detail');
        var ulHtml = '<li>User ID city: '+user.city+'</li>' + '<li>User Person'+user.sex+'</li>' + '<li>User ID: '+user.uid+'</li>' + '<li>User Status: '+user.status+'</li>' + '<li>User Online/Offline: '+user.online+'</li>';

        $detail.find('img').attr('src', user.photo_big);
        $detail.find('h3').text(user.first_name + ' ' + user.last_name);
        $detail.find('ul').html(ulHtml);
        $detail.find('button').attr('data-uid', user.uid);

        $detail.show();
    }

    function sendMessage(event) {
        var uid = +$(event.target).attr('data-uid'); 
        var value = $('textarea').val();

        if (!value) {
            alert("Не ввели сообщение");
            return;
        }

        sendRequest('messages.send', {user_id: uid, message: value}, function() {
            console.log('Отправлено');
        });
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question