Error with the use of AJS.$.ajax
Asked by Paulo Castelo:

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


Answers:
author: Steve Goldberg
body: 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.:

```
&lt;script type="text/javascript"&gt;AJS.toInit(function(){jQuery.ajax({ ... });});&lt;/script&gt;
```

Hope this helps.

comments: author: Steve Richards
body: Steve - thank you so much!

I now have this working with a get and post.


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


author: Paulo Castelo
body: 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...


author: Nadir MEZIANI
body: Hi,

I think this example [](http://www.j-tricks.com/1/post/2012/02/some-ajs-tricks.html)<http://www.j-tricks.com/1/post/2012/02/some-ajs-tricks.html>works only in **JIRA**.


author: RambanamP
body: try with this script

```
&lt;script type="text/javascript"&gt;(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.$);&lt;/script&gt;
```

or tey with following script

```
&lt;script type="text/javascript"&gt;    AJS.toInit(function(){                 alert(AJS.params.remoteUser);            });&lt;/script&gt;
```

check this dco for more info

[](https://confluence.atlassian.com/display/CONFKB/How+to+Use+JavaScript+in+Confluence)<https://confluence.atlassian.com/display/CONFKB/How+to+Use+JavaScript+in+Confluence>

comments: author: Paulo Castelo
body: Thanks a lot for your prompt answers guys. Unfortunately the error continues...


author: RambanamP
body: can you share your code ?

how you are adding javascript on confluence?


author: Paulo Castelo
body: 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](https://docs.atlassian.com/atlassian-confluence/REST/latest/#d2e377)' 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...


author: RambanamP
body: i have updated my answer!! try with that!


author: Steve Richards
body: 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?


author: Nadir MEZIANI
body: 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;}
```


Tags: ajs

macro

user