我无法使用Ajax通过Rest API发布Yammer [英] I can not post Yammer via Rest API using Ajax

查看:74
本文介绍了我无法使用Ajax通过Rest API发布Yammer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个JavaScript应用,该应用使用Yammer REST API将消息发布到Yammer.但我有一个限制.我无法使用Yammer SDK发布消息.所以,我写了ajax的代码.

Hi I would like to Create JavaScript app which post message to Yammer, using Yammer REST API. but I have a limitation. I Can Not use Yammer SDK to post message. so, I written code of ajax.

但是,在此代码中不起作用.XMLHTTPrequest始终拒绝访问".

However, In this code Doesn't work. XMLHTTPrequest is always "access denied".

为什么此代码不起作用?

why this code doesn't work?

我的Yammer网络允许第3方应用程序.

MY Yammer network allow 3rd party app.

function sendData() {

    var messagebody = new Object();
    messagebody.body = "This is test";
    messagebody.group_id ="4627253"
    
    //get accToken 
    accToken = responseObject.access_token.token;
    var accAuthHead = "Bearer "+ accToken;

    $.ajax({
        url: "https://www.yammer.com/api/v1/messages.json",
        type: 'POST',
        headers: {
            'Accept': 'application/json, text/javascript, */*; q=0.01',
            'Accept-Encoding': 'gzip, deflate',
            'Authorization': accAuthHead.toString(),
            'X-Requested-With': 'XMLHttpRequest',
        },

        data: JSON.stringify(messagebody),
        datatype: "json",

        success: function (data) {
            console.log("ajax post success.");
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert("There was an error with the request." + textStatus.toString()+ XMLHttpRequest.toString());
        }
    });


}

推荐答案

我尝试了您的代码,发现了两个可能导致您遇到问题的严重问题:

I've tried your code and found two serious issues that might cause your problems:

  1. 您应该使用https:// api .yammer.com/api/v1/messages.json而不是https:// www .yammer.com/api/v1/messages.json

  1. You should use https://api.yammer.com/api/v1/messages.json instead of https://www.yammer.com/api/v1/messages.json

不要JSON.请对您的数据进行字符串化处理,因为它应该以JSON(而不是字符串)形式发送.

Don't JSON.stringify your data as it's supposed to be sent as just that, JSON, not as a string.

希望有帮助!

这篇关于我无法使用Ajax通过Rest API发布Yammer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆