Forums

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

User Macro & AJAX

Dong Park
Contributor
January 8, 2020

Hello,

 

I'm trying to get data from external API(non-Atlassian software) using user-macro feature.  All the examples that I see and working are all getting API data from either Confluence or Jira.  I'm wondering if user-macro feature with ajax doesn't work with external API.  

Disclaimer:  I'm new to Confluence. 

Here is a sample:

https://community.atlassian.com/t5/Answers-Developer-Questions/AJAX-call-to-external-API-in-user-macro/qaq-p/494128

## @param Name:title=Name|type=string|required=true|desc=Your name
<script type="javascript>
AJS.$.ajax({
  url: URL_WITH_NO_QUOTES,
type: "GET",
contentType: "application/json",
  success: function(msg){
    alert(msg);
  }
});
</script>

 Thanks!

1 answer

0 votes
Zak Laughton
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 10, 2020

Hi Dong!

It looks like an answer in the community link you posted might have the answer you're looking for:

I suppose $ is treated as a special character used by Velocity template. So, you need to escape it.I hope the following code will work.

## @param Name:title=Name|type=string|required=true|desc=Your name

#set($D='$')

<script type="text/javascript">
AJS.${D}.ajax({
  url: URL_WITH_NO_QUOTES,
type: "GET",
contentType: "application/json",
  success: function(msg){
    alert(msg);
  }
});
</script>

Besides, <script type="javascript> in your code should be quoted properly.

If the updated script above doesn't work, can you describe what happens when you try to use it? It will also help to know if you see any new network calls when you load the macro.

To do this:

  1. Open the page.
  2. Open your browser's dev tools to the "Network" tab
  3. Refresh the page
  4. You will see the network tab populate with all the network calls. You can check here to see if a request was made to the desired URL.

I hope this helps!
– Zak

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events