It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

User Macro & AJAX

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 Jan 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
Community showcase
Posted in Confluence

How is your team having fun and bonding, remotely, utilizing Confluence?

Thanks everyone for answering last week’s question. The winner of the random drawing from those who commented is: @LarryBrock I’ll contact you separately with your prize details. This wee...

327 views 9 7
Join discussion

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you