Howto get and post data via jQuery from JIRA REST API?

Dan December 1, 2015

We are using JIRA 6.4.5 in our company but I am struggeling fetching data from its API REST interface. I have been trying now for the last couple of days, getting stuck on a cross-domain problem or that I don't know the user credentials so I cannot do any server-side either.

Ideally I am having a jQuery page where the user will use his own credentials/session for querying the JIRA data. The JIRA REST API is located at srv1.mydomain.xyz and I am using srv2.mydomain.xyz as my webserver with my code.

I have read the JIRA REST API Reference.

I have tried various Javascript/jQuery stuff - in the below example I am trying to submit 1h 30minutes to a specific issue:

$.ajax({
    url: "https://srv1.mydomain.xyz/rest/api/latest/issue/proj-3/worklog",
    dataType: "json",
    method: "post",
    data: { time: "1h 30m",
    comment: "Test" }
}).done(function(data) {
    alert("Success");
}).fail(function(jqXHR, textStatus) {
    alert("Failed");
});

I get this error:

XMLHttpRequest cannot load https://srv1.mydomain.xyz/rest/api/latest/issue/proj-3/worklog?time=1h+30m&comment=Test. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://srv2.mydomain.xyz' is therefore not allowed access. The response had HTTP status code 401.


I then looked more in to this and saw that Atlassian has something called Atlassian Connect so I tried with this:

AJS.$.ajax({
    url: "https://srv1.mydomain.xyz/rest/api/latest/issue/proj-3/worklog",
    type: "post",
    dataType: "json",
    contentType: "application/json",
    xhrFields: { withCredentials: true },
    async: false,
    method: "post",
    data: { time: "1h 30m",
    comment: "Test" }
}).done(function(data) {
    alert("Success");
}).fail(function() {
    alert("Failed");
});


But I get a similar error:

XMLHttpRequest cannot load https://srv1.mydomain.xyz/rest/api/latest/issue/proj-3/worklog. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://srv2.mydomain.xyz' is therefore not allowed access.


I have also looked in to if I could do this server-side from my PHP enabled server in the same domain as the JIRA server but as I don't get the base64 encoded credentials when doing a phpinfo() then I don't think I can use this approach either (and I don't want to prompt the user for credentials).

I am painfully aware that my problem is related to cross-domain protection but I cannot find any examples on how to fix it? It would be great if the JIRA server could set a Access-Control-Allow-Origin for certain hosts but I assume this is not a configuration option (I am not in control of the JIRA server).

Can anyone give some hints how to proceeed?

1 answer

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Volodymyr Krupach
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.
December 1, 2015

Hi Dan,

Adding you srv2.mydomain.xyz  to you JIRA whitelist should solve the cross-domain error.

TAGS
AUG Leaders

Atlassian Community Events