Access JIRA REST API from Confluence via AJAX

Tim July 16, 2013

If I run the following script from JIRA it work:

$.ajax({
	url : "http://localhost:8080/jira/rest/api/2/project",
	type : "GET",
	dataType : "json",
	contentType : "application/json",
	success : function(response)
	{
		console.log(response);
	},
	error : function(jqXHR, textStatus, errorThrown)
	{
		console.log(textStatus + " : " + errorThrown);
	}
});

...however if I run it from Confluence it doesn't work. The error function gets "error" for the textStatus and nothing for errorThrown.

I have a Trusted Application link setup between JIRA and Confluence.

Is it possible or not possible to do this via JavaScript?

5 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Joe Clark
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 17, 2013

It is not possible to do this in JavaScript due to your browser's security policy (see http://en.wikipedia.org/wiki/Same_origin_policy). If you have root access on your JIRA server, you may be able to work around this by configuring your JIRA server's Cross-Origin Resource Sharing (CORS) Policy - this answer has some instructions on how you can attempt it: https://answers.atlassian.com/questions/69356/cross-origin-resource-sharing-with-jira-rest-api-and-javascript

Alternatively you can build a Java plugin that runs on your Confluence server to make the JIRA REST calls, and then your JavaScript can call a custom REST resource supplied by your Confluence plugin to retrieve the information from JIRA.

Tim July 18, 2013

I suspected something like that was the case. Thanks for the confirmation.

I had already started doing option 2 before I posted this... so I'll just stick with that method.

Thanks!

4 votes
Kai November 5, 2013

Try this!

Works for me in my local development system: localhost:8090 and localhost:8080

var jira = [];
var projects = [];

var request = $.ajax({
url: "/rest/jiraanywhere/1.0/servers",
type: "GET",
async: false,
dataType: "json"
});

request.done(function( msg ) {
$.each( msg , function( key, val ) {
if(val.name && val.name.contains('JIRA')){
jira.push(val);
return true;
}
});
});

request.fail(function( jqXHR, textStatus ) {
console.log( "Request1 failed: " + textStatus );
});

if(jira.length > 0){
var requestProjects = $.ajax({
url: "/plugins/servlet/applinks/proxy?appId="+jira[0].id+"&path="+jira[0].url+"/rest/api/2/project",
type: "GET",
async: false,
dataType: "json"
});

requestProjects.done(function( msg ) {
$.each( msg , function( key, val ) {
if(val.name){
projects.push(val);
}
});
});

requestProjects.fail(function( jqXHR, textStatus ) {
console.log( "Request2 failed: " + textStatus );
});

}

console.log( "projects: " + projects.length );

Johannes Moser April 27, 2015

This is awesome!

chrisonare May 9, 2016

Hi kai,

I am in exactly the same dilemma, I have a confluence blueprint that i am trying to get all JIRA projects in a dropdown but it is not working. I have tried implementing your code above and tied the projects array to the dropdown but it is not working. I cannot see any console logs at all.  My code is:

$(document).ready(function() {
$(".project").select2({
data: projects
});
});

Any ideas?

thanks in advance

 

Sandra Mulligan May 18, 2016

Hi Chris,

did you have any luck with getting all JIRA projects list in your confluence blueprint?

thanks,

Sandra

chrisonare May 19, 2016

Hi Sandra,

I finally managed to get it working using Kai's method but also using the Confluence.getContextpath().

Kind regards, 

chris

Thomas Strauss February 12, 2017

Hello Chris,

how did you manage the authorization step? when I try to access a JIRA api via the application link path I always get a unauthorized exception even tough I use the same admin account on both instances? do you add the current logged in user to the request call? how do you get the current user? Please help me smile

Best regards,

Tom

abhay May 22, 2019

Hello Chris,

Can you please share your entire code snippet

where exactly you used Confluence.getContextpath().

1 vote
LucasA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 16, 2013

Hi,

I'm not sure if you really can do this via JavaScript, but Confluence already has a macro for querying JIRA projects. It's enabled by default when creating an application link from JIRA to Confluence.

Best regards,
Lucas Timm

0 votes
shyamal deori September 19, 2014

Hi,

Is there any AJAX call that I can use to get all the teams the current logged in user belongs to?

 

Thank You,

Asif

0 votes
Ian Bantilan November 28, 2013

Can I do same for onDemand? I need to be able to query the weblinks of a JIRA issue as weblinks don't showup in issue navigator

Jay Allison September 23, 2016

According to the response I got from Atlassian on ticket https://ecosystem.atlassian.net/browse/AC-2129, this is still not supported in Cloud. They pointed me to this feature request:

 

https://ecosystem.atlassian.net/browse/AC-2001

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events