Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Error with the use of AJS.$.ajax

Paulo Castelo January 6, 2014

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

4 votes
Steve Goldberg
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.
January 13, 2014

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 Richards
Contributor
January 13, 2014

Steve - thank you so much!

I now have this working with a get and post.

Alibek July 13, 2016

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

0 votes
Paulo Castelo January 7, 2014

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

0 votes
Nadir MEZIANI
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.
January 6, 2014

Hi,

I think this example http://www.j-tricks.com/1/post/2012/02/some-ajs-tricks.htmlworks only in JIRA.

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.
January 6, 2014

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

Paulo Castelo January 6, 2014

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

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.
January 6, 2014

can you share your code ?

how you are adding javascript on confluence?

Paulo Castelo January 7, 2014

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

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.
January 7, 2014

i have updated my answer!! try with that!

Steve Richards
Contributor
January 13, 2014

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?

0 votes
Nadir MEZIANI
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.
January 6, 2014

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events