【问题标题】:How to resolve cross domain error in REST Api while posting on specific group in yammer如何在 yammer 中的特定组上发布时解决 REST Api 中的跨域错误
【发布时间】:2015-07-07 03:41:26
【问题描述】:

我尝试在 yammer 的特定组中发布以下代码。

function post() {
yam.getLoginStatus(
    function(response) {
        if (response.authResponse) {
            console.log("logged in");
            console.dir(response); //print user information to the console
            yam.platform.request({ 
                        url: "https://www.yammer.com/api/v1/messages.json"
                        , method: "POST"
                        , data: { "body" : "Welcome to the Yammer API World.", "group_id":"xxxxxxx"}
                        , CORS: true
                        , dataType: "json"
                        , headers: { "Accept": "application/json; odata=verbose" }
                        , xhrFields: { withCredentials: true }
                        , success: function (msg) { alert("Post was Successful!: " + msg); }
                        , error: function (msg) { alert("Post was Unsuccessful..." + msg); }
                    });
        }
        else {
            yam.platform.login(function (response) {
                if (response.authResponse) {
                   console.dir(response);
                    yam.platform.request({ 
                        url: "https://www.yammer.com/api/v1/messages.json"
                        , method: "POST"
                        , data: { "body" : "Welcome to the Yammer API World.", "group_id":"xxxxxxx"}
                        , CORS: true
                        , dataType: "json"
                        , headers: { "Accept": "application/json; odata=verbose" }
                        , xhrFields: { withCredentials: true }
                        , success: function (msg) { alert("Post was Successful!: " + msg); }
                        , error: function (msg) { alert("Post was Unsuccessful..." + msg); }
                    });
                }
            });
        }
    });

}

在 HTML 文件中我添加了这段代码

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

上面的代码抛出错误为

我有什么遗漏的吗?

【问题讨论】:

    标签: javascript rest sharepoint-2013 yammer


    【解决方案1】:

    与 JS 无关。您必须在服务器标头中添加跨源。 您必须在服务器标头中执行类似的操作

     header("Access-Control-Allow-Origin: *")
    

    【讨论】:

    • 如何在其中添加服务器标头
    • 你能简单解释一下吗?我不知道在哪里放置 header("Access...")
    • 这个答案中给出的代码是 PHP。这个问题被标记为 sharepoint-2013,所以我看不出 PHP 的相关性。
    • @sandeep.gosavi——假设是PHP生成的,你得把它放在负责生成https://www.yammer.com/api/v1/messages.json的服务器端代码中
    • messages.json 由 yammer 自己创建。
    【解决方案2】:

    我对网址进行了更改。更改之前我的网址是

     url: "https://www.yammer.com/api/v1/messages.json"
    

    现在我的网址是

    url: "https://api.yammer.com/api/v1/messages.json"

    一切正常

    我的 2 天只用了三个字符....

    【讨论】:

    • 赞成感谢是否可以使用此方法获取 cmets 的数量?
    【解决方案3】:

    如果有帮助,这里有关于如何在 yammer 中向群组发布消息的完整示例代码 - http://blogs.technet.com/b/israelo/archive/2014/10/21/yammer-js-sdk-for-dummies.aspx

    【讨论】:

      猜你喜欢
      • 2018-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-20
      • 1970-01-01
      • 1970-01-01
      • 2021-01-14
      相关资源
      最近更新 更多