Can't Use Json to Access JIRA API

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

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!!!

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.

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;		
	});
});

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;
}

is it triggering javascript function?

are you getting any error message?

which version of jira you are using?

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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Jira

Meet the AUG leaders of Northern Virginia

@Rachel Wright (Jira Genie), @Billy Poggi (AUG NOVA, DC), and @Dana Jansen (Confluence Queen) are just some of the folks that lead one of the world's most active Atlassian User Group (AUG)....

158 views 5 9
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you