在 yammer 中的特定组上发帖时如何解决 REST Api 中的跨域错误

How to resolve cross domain error in REST Api while posting on specific group in yammer

我在 yammer 的特定组中尝试了以下代码 post。

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>

上面的代码抛出错误

里面有我遗漏的东西吗?

与JS无关。您必须在您的服务器 header 中添加交叉来源。 你必须在服务器 header

中做这样的事情
 header("Access-Control-Allow-Origin: *")

我在 url 中进行了更改。更改前我的 url 是

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

现在我的 url 是

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

工作正常

只有三个字符花了我 2 天....

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