Can't Use Json to Access JIRA API

Mengna Du July 16, 2013

Hi,

I am developing a standalone gadget for JIRA with HTML and JavaScript. In which I wish to use json to get the username of current via REST API and owner of dashboard to realize the authorization.

According to https://answers.atlassian.com/questions/32731/ajax-calls-in-jira-5-0

I use a similar way to access the database. Nevertheless it does not work - the success function never works.

For the url, I use the whole name - i.e. http://.../currentUser, which could actually work via XMLHTTPRequest - but with XMLXttpRequest I could not fetch the value of a single field....So it is not usable.

Could you please help me to answer this question?

Thanks!

2 answers

1 accepted

0 votes
Answer accepted
Mengna Du July 18, 2013

There is the problem: According to the official cite, AJAX is not supported in JIRA any more. So to deal with this issue, I use jQuery instead.

To use the jQuery syntax, simply include this line before any other scripts in the file.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>

Then, I implement the functionality in the following way:

$(document).ready(function(){
    $("#test").click(function(){
      alert("I am here");
          $.getJSON("/jira/rest/gadget/1.0/currentUser",function(result){
            alert(result.username);
          });
  });
});

The usage is referred from http://www.w3schools.com/jquery/ajax_getjson.asp

Finally it works!!!!!

P.S. Chrome's Javascript Console is so useful! Highly recommended!!!

John Nguyen February 25, 2018

Hi Menga

  I'm new to the whole thing ( JSON, JQUERY, AJAX,etc..,) but I'm technically capable of understanding how things work once I see the code.

Would you kind enough to give me the whole JavaScript file ( a simple sample will do ) where you make the API call to Jira?

1 - How the HTML file looks like?

2 - How the JavaScript looks like?

3 - Did you need to provide the user credentials (<userID>:<password> in base 64 basic authentication)?  If you did, then how did you do that? If you don't, then how did your Jira allow you to retrieve the user data?

I'm trying to build an example where my javascript will make API calls to my Jira.  Once I get the returned result in JSON  format I can parse the result and pump the data to an HTML running on my local HTML.

Thanks a lot for your help

J.N

PS: you could email me your sample code to my email: nguyen7744@yahoo.com.

0 votes
RambanamP
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

i think you are trying to get current login user name

if yes then try with following code

jQuery(document).ready(function() {
	AJS.$.get("/jira/rest/auth/1/session", function (data) {	
		var userName = data.name;		
	});
});

Mengna Du July 17, 2013

Hi, thank you for your answer!

I have tried but it does not work as well....here is the code:

function getCurrentUserName()
{
alert("function begins!");
var userName;
  jQuery(document).ready(function() {
      AJS.$.get("http://ltsjc-monadu:2990/jira/rest/auth/1/session", function (data) {  
          userName = data.name;     
      });
  });
alert(userName);
return userName;
}

RambanamP
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 17, 2013

is it triggering javascript function?

are you getting any error message?

RambanamP
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 17, 2013
RambanamP
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 17, 2013

which version of jira you are using?

Mengna Du July 17, 2013

yep, it is in JavaScript. But I did not get any error message... I believe it is JIRA 5.1.7. Oh you just reminded me to test it in HTML rather than Gadget. Let me try it and then post the follow-up.

Thanks!

Suggest an answer

Log in or Sign up to answer