Error with the use of AJS.$.ajax

Hi.

I'm following the same code I've found in other sites to check the groups for the current user (namely on http://www.j-tricks.com/1/post/2012/02/some-ajs-tricks.html) in Confluence and the following render error is always returned: "Error rendering macro 'macro-name' : Error occurred rendering template content".

Here's the script:

AJS.toInit(function() {

var username = getCurrentUserName();
(... some more script ...)
});

function getCurrentUserName() {
var user;
AJS.$.ajax({
url: "/rest/gadget/1.0/currentUser",
type: 'get',
dataType: 'json',
async: false,
success: function(data) {
user = data.username;
}
});
return user;
}

I debugged the script and the error happens on the AJS.$.ajax call. I tested the URL directly to check if the current user info is being returned and it is.

Tried some alternatives, but without any luck. The error happens everytime the AJS.$ is called. If I remove that block everything works fine.

Does anyone has some suggestion on how to solve this (or bringing me a bit closer to the solution)?

Thank you.

5 answers

Hi,

I've used AJAX in a user macro as I use it to pull in a 'feedback' button which launches a JIRA issue collector. The thing I found is that you can't use $.ajax for some reason but using jQuery.ajax did work. E.g.:

<script type="text/javascript">
AJS.toInit(function(){
jQuery.ajax({ ... });
});
</script>

Hope this helps.

Steve - thank you so much!

I now have this working with a get and post.

Thanks! That's helped me too! I tried AJS.$.ajax - and it's did not worked for some reason.

Hi,

Add to your url contextPath it will be like this
url: contextPath+"/rest/gadget/1.0/currentUser"

function getCurrentUserName(){
var user;
AJS.$.ajax({
url: contextPath+"/rest/gadget/1.0/currentUser",
type: 'get',
dataType: 'json',
async: false,
success: function(data) {
user = data.username;
}
});
return user;
}

try with this script

<script type="text/javascript">
(function($) {
	AJS.toInit(function() {
		var username = getCurrentUserName();
		alert('loggedin user: '+username);
	});

function getCurrentUserName() {
	var user;
	AJS.$.ajax({
		url: "../rest/gadget/1.0/currentUser",
		type: 'get',
		dataType: 'json',
		async: false,
		success: function(data) {
			user = data.username;
		} 
	});
return user;
}
})(AJS.$);
</script>

or tey with following script

<script type="text/javascript">
    AJS.toInit(function(){
       
          alert(AJS.params.remoteUser);
        
    });
</script>

check this dco for more info

https://confluence.atlassian.com/display/CONFKB/How+to+Use+JavaScript+in+Confluence

Thanks a lot for your prompt answers guys. Unfortunately the error continues...

can you share your code ?

how you are adding javascript on confluence?

Well... is prety much as I wrote in the post and it's inside a macro. The rest of the script is just code to show a popup window. The idea is to show the popup just when the user belongs to a specific group. That's why I picked up this example, it would work just fine with the rest.

I even tried with the '/rest/prototype/1/user/current' URL (got it from the Confluence REST API documentation) and it works just fine on a direct call in the browser.

The problem is really the use of the AJS.$ on the script and I can't understand why. If I add the AJS.$.ajax it results the error (even without setting any parameters), I remove it and everything works...

Am I missing some assembly call or something? Is there any other way to get the current user and the groups to which he belongs? I am pretty new working with the Confluence API, so some things just surpass me...

i have updated my answer!! try with that!

Hi,

Did you get this working as I am facing the same issue when using AJS.$.post in a Confluence User Macro.

Is the use of AJAX in a user macro not supported?

Hi Zezeto. Thanks for your comment. The example is for JIRA, true, but if I make the call on the browser it results the information about the current user. I took that if it results the information the example should work as well in Confluence.

But I used as well the Confluence REST URL to get the current user information and it returns the information with sucess on the browser not on the script. Even tried with the full URL, but to no avail...

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
Posted Jul 10, 2018 in Confluence

We want to see the templates you've created in Confluence!

Hi Community, Jessica here from the Confluence Product Marketing team!  July’s community challenge is all about sharing pictures  — and as an extension of our first post on what ...

602 views 20 11
Join discussion

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