获取yammer feed中的评论数量

时间:2015-11-27 05:32:49

标签: javascript jquery sharepoint yammer

我正在尝试获取yammer Feed的评论数量。

我使用了以下查询

jQuery.getScript("https://c64.assets-yammer.com/assets/platform_js_sdk.js",function(){
console.log("script loaded");
  var commentCnt = 0;
      yam.platform.request(
          { url: "https://www.yammer.com/api/v1/messages/open_graph_objects/"+ogID+".json"
          , method: "GET"
          , data: {"body": "This Post was Made Using the Yammer API.  Welcome to the Yammer API World."}
          , success: function (msg) {
                    if (msg) {
                        jQuery.each(msg.messages, function (index, element) {
                            commentCnt++;
                        });
                    }
                    //adds the count to the webpage.
                    jQuery("div#commentCnt").text(commentCnt);
                }
          , error: function (msg) { 
                        //console.log("message lookup failed");
            }
      });
});

此返回

XMLHttpRequest cannot load http://myApiUrl/. No
'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'null' is therefore not allowed access

如何解决这个问题?我还尝试使用api.yammer.com代替www.yammer.com

1 个答案:

答案 0 :(得分:1)

Yammer CORS错误主要是由于缺少JS来源。您需要在代码中定义data-app-id。看起来你的片段中没有...

data-app-id="YOUR-APP-CLIENT-ID" 

data-app-id,是您注册的应用程序的app_id,并确保JS原点到位。

另外,我建议您不要使用jQuery.getScript(),而是建议您在HTML页面的标题部分调用脚本,如下所示:

<script type="text/javascript" data-app-id="YOUR-APP-CLIENT-ID" src="https://c64.assets-yammer.com/assets/platform_js_sdk.js"></script>