jquery - 通过 AJAX(隐藏 Api)获取 google +1 页面共享

标签 jquery ajax cross-domain google-plus

努力寻找通过 jQuery - Ajax 从 Google 的隐藏 api 获取 Google Plus +1 页面的解决方案:https://clients6.google.com/rpc

此问题也在以下位置进行了讨论:Stackoverflow link

我的尝试:

$.ajax({
    cache: false,
    type: "POST",
    url: "https://clients6.google.com/rpc",
    data: [{
        "method":"pos.plusones.get",
        "id":"p",
        "params":{
            "nolog":true,
            "id":"http://www.apple.com",
            //"id":"http%3A%2F%2Fwww.apple.com",
            "source":"widget",
            "userId":"@viewer",
            "groupId":"@self"
            },
            "jsonrpc":"2.0",
            "key":"p",
            "apiVersion":"v1"
    }],
    crossDomain: true,
    jsonp: true,
    timeout: 5000,
    dataType: "jsonp",
    contentType: "application/json; charset=utf-8",
    success: function (data) {
        console.log(data);
    },
    always: function(data){
        console.log(data);
    }
});

chrome 中的结果:未捕获的语法错误:意外的标记:

在 Firefox 中: 语法错误:缺少;之前的声明

{"error":{"code":-32700,"message":"解析错误","data":[{"domain":"g

有什么想法可以解决这个问题吗?

最佳答案

您可以使用 google plus javascript 库来获取分享计数:

包括这些:

<script src="https://apis.google.com/js/plusone.js"></script>
<script src="https://apis.google.com/js/client:plusone.js"></script>

然后执行:

var params = {
  nolog: true,
  id: "http://www.google.com/",
  source: "widget",
  userId: "@viewer",
  groupId: "@self"
};

gapi.client.setApiKey('AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ')
gapi.client.rpcRequest('pos.plusones.get', 'v1', params).execute(function(resp) {
  console.log('count:', resp.result.metadata.globalCounts.count)
});

请勿将 apikey 替换为您自己的。如果你这样做,它就不会起作用。

关于jquery - 通过 AJAX(隐藏 Api)获取 google +1 页面共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21524077/

相关文章:

javascript - 无法重置所选选项值

javascript - ajax 请求后的 Ajax,以正常方式提交表单

jquery - 将 Backbone Js 与 jQuery Deferred 结合使用

javascript - 如何从 XMLHttpRequest.responseText 解析 HTML 属性值?

javascript - 表单未发布资源未找到

PHP-jQuery : Display a timer on each table row where end time is not set

iframe - 嵌套在非安全页面上的安全 IFRAME

javascript - Highcharts 使用 css 类将自定义样式应用于上下文按钮

php - 安全灵活的跨域 session

jquery - 与 Post 进行交叉调用,但在飞行前失败