-1

I try to get share count from Mail.ru social network (Мой Мир).

If I go to browser, put this URL http://connect.mail.ru/share_count?url_list=THIS_MY_URL&callback=1&func=? and press enter, I'll get this response:

?(
{
  "THIS_MY_URL":{"shares":10,"clicks":5}
});

This is my jQuery code:

var url = encodeURIComponent(location.href);

$.getJSON('http://connect.mail.ru/share_count?url_list=' + url + '&callback=1&func=?', function(response) {
  alert(response[url].shares);
});

In Chrome console I get this message:

Uncaught TypeError: Cannot read property 'shares' of undefined

Whats wrong? How to make it work?

Help me please!

Bogdan Burym
  • 5,482
  • 2
  • 27
  • 46
Platon
  • 495
  • 1
  • 4
  • 11
  • Can you show the json data you are receiving in response.Is there shares property ? –  Sep 15 '15 at 17:03
  • For example, URL in query is http://vk.com — JSON data is: ``?({ "http:\/\/vk.com": {"shares":12372, "clicks":413} });`` – Platon Sep 15 '15 at 17:08
  • This is not valid json as per http://jsonlint.com/ .It shows { "http://vk.com": { "shares": 12372, "clicks": 413 } } –  Sep 15 '15 at 17:10
  • Look this link please: https://github.com/sapegin/social-likes/blob/master/src/social-likes.js#L61-L69 They parse (or something else) this not valid JSON to share count. How to do this on my code (at first message)?! – Platon Sep 15 '15 at 17:16

2 Answers2

0

See here I am getting the value.

    var data = { "vk.com": { "shares": 12372, "clicks": 413} };

        for (var url in data) {
            if (data.hasOwnProperty('vk.com')) {
                var shares= data['vk.com'].shares;
            }
        }

Debugging Poster

  • OK. This work to ONE url. But If I don't know what url to be in link? For example, url is ``encodeURIComponent(location.href)`` — then this code don't work. – Platon Sep 15 '15 at 17:29
  • Can you show me then a real json data with multiple case. –  Sep 15 '15 at 17:31
  • also look at this link http://stackoverflow.com/questions/18238173/javascript-loop-through-json-array –  Sep 15 '15 at 17:33
  • Hmm.. jQuery Code in my first (start) message — how to get your code to them? – Platon Sep 15 '15 at 17:36
  • Maybe need parse not valid JSON response (for ex. ``?({ "http:\/\/vk.com": {"shares":12372, "clicks":413} });`` or any) to valid and next try to ``for(...)`` from your code? How? – Platon Sep 15 '15 at 17:40
0

Worked solution I found here.

First. Error message Uncaught TypeError: Cannot read property 'shares' of undefined showen, when response Object is empty. This is standard response from social network, when URL have not share count, i.e. Object = {}.

For fix it, put this code in $.getJSON() (from my first post):

if ($.isEmptyObject(response)) alert('0'); // If Object = {}
else // Code for not empty Object

Second. For show counter for URL with shares > 0 (or clicks > 0), use this code:

var url = encodeURIComponent(location.href);
for (var url in response) {
    if (response.hasOwnProperty(url)) {
        var count = response[url].shares;
        break;
    }
}

alert(count);
Platon
  • 495
  • 1
  • 4
  • 11