Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How do I make rest calls with JavaScript

Kessy Similien
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
December 7, 2018

I'm trying to make REST calls using JS and the documentation is not helping me at all. 



$(function() {
console.log("Ready");

const ax = axios.create({
baseURL: "https://r3cloud.atlassian.net/rest/api/3",
headers: {
Authorization: `Basic ${btoa("username:apitoken")}`,
"Content-Type": "application/json"
}
});

ax.get("/myself")
.then(console.log)
.catch(console.log);

$.ajax({
url: "https://r3cloud.atlassian.net/rest/api/3/myself",
type: "GET",
dataType: "json",
success: function(data) {
console.log(data);
},
error: function() {
console.log("boo!");
},
beforeSend: setHeader
});
function setHeader(xhr) {
xhr.setRequestHeader("Authorization", "Basic username:apitoken");
xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
}
});

 

But that fails with: 

 

Access to XMLHttpRequest at 'https://mysite.atlassian.net/rest/api/3/myself' from origin 'https://mysite.sharepoint.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

 

 

1 answer

1 vote
Dario B
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
December 11, 2018

Hi @Kessy Similien,

The error you are getting is related to CORS. In below Feature Request ticket I can read that support for CORS has been added only for "calls to Jira via api.atlassian.com using (3LO) access tokens":

https://jira.atlassian.com/browse/JRACLOUD-30371

 

...

Please be aware that this only affects calls to Jira via api.atlassian.com using (3LO) access tokens. I'll add additional information later today...

...

...Thanks for your patience everyone. I've personally now tested things with my example app and it works as expected!

If you haven't yet, please get yourself familiar with the documentation here: https://developer.atlassian.com/cloud/jira/platform/oauth-2-authorization-code-grants-3lo-for-apps/

You can use the "Give docs feedback" on the docs page should anything be unclear or not work as described/expected.

Please also note that there's no support for the implicit grant flow yet, so client-side only apps will still need for that to be added....

....

 

Please review above FR ticket and the linked documentation. Then, in case you still have problems, I believe it would be much better to go ask for help in the developers' community or in ecosystem since this is a development related issue and those are the best resources where to ask for help for this:

Suggest an answer

Log in or Sign up to answer