I am getting the following error while trying to run this code:
function getCurrentUser() {
yam.platform.request({
// yam.request({
url: "users/current.json", //this is one of many REST endpoints that are available
method: "GET",
data: {},
success: function (user) { //print message response information to the console
console.log("User request was successful.");
console.dir(user);
toggleLoginStatus(true);
$('#authResult').html('User Result:<br/>');
for (var field in user) {
$('#authResult').append(' ' + field + ': ' +
escape(user[field]) + '<br/>');
}
},
error: function (user) {
console.error("There was an error with the request.");
}
});
}
Error: XMLHttpRequest cannot load https://api.yammer.com/api/v1/users/current.json?_=1407933095976. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://brillioonline.sharepoint.com' is therefore not allowed access.
I am trying to use the following code in order to solve the error, but have no clue what token is here:
$.ajax(
'https://app.asana.com/api/1.0/users/me',
{
type: 'GET',
dataType: 'json',
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "Bearer $token")
},
complete: function (resp) {
console.log(resp);
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(textStatus);
}
}
);
Please help me with this, i am a beginner
First of all, you don't need to use Ajax or set the header mannually. Yammer js sdk does it for you. But if you really want to use Ajax, you can get the token from the response of getLoginStatus
(see below)
To get current user without Ajax, make sure you have logged in. Here is an example:
function login() {
yam.platform.getLoginStatus(function (response) {
if (response.authResponse) { //if already logged in
alert("token=" + response.access_token.token);
} else {
yam.platform.login(function (response) {
if (response.authResponse) {
alert("token=" + response.access_token.token);
}
});
}
});
}
2. After a successful login, run your request. If it still doesn't work, run the request inside a getLoginStatus
block:
yam.platform.getLoginStatus(function (response) {
if (response.authResponse) {
//RUN YOUR REQUEST HERE.
} else {
alert("something went wrong");
}
});