Confluence Plugin Development Javascript
Asked by Mustafa Abusalah:

I'm trying to retrieve a variable using ajax call. The code is working fine outside confuence. But inside confluence it is not working.

JS file is below:

```
// JavaScript Document
// badges menu
AJS.toInit = function (func) {
?? ?$(function() {?????????????? ?
?? ??? ?$.ajax({
?? ??? ??? ?url : 'data.txt',?? ?
?? ??? ?}).done(function(data){
?? ??? ??? ?// $('#newLessonsBadge').html(data);
?? ??? ??? ?? var obj = data;
?? ??? ??? ?? if (obj.Success == 'true'){
?? ??? ??? ?? $('#totalNotificationsBadge').html(obj.totalNotifications);
?? ??? ??? ?? }
?? ??? ??? ?? else{
?? ??? ??? ?? //$('#feedbackReject').html('<div class="col-lg-12" style="width:100%;margin:0;padding:0"><div class="alert alert-dismissible alert-danger"><button type="button" class="close" data-dismiss="alert">&times;</button><strong>Error!</strong> '+obj.Message+' Data Was not Saved</div></div>');
?? ??? ??? ?? }
?? ??? ?}).fail(function(){

?? ??? ?});
?? ?}); 
??? return this;
};
```

Java code:

```
package com.atlassian.tutorial.macro;

import com.atlassian.confluence.content.render.xhtml.ConversionContext;
import com.atlassian.confluence.macro.Macro;
import com.atlassian.confluence.macro.MacroExecutionException;
import java.util.Map;
import com.atlassian.plugin.spring.scanner.annotation.component.Scanned;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import com.atlassian.webresource.api.assembler.PageBuilderService;
import org.springframework.beans.factory.annotation.Autowired;
@Scanned
public class helloworld implements Macro {

??? private PageBuilderService pageBuilderService;

??? @Autowired
??? public helloworld(@ComponentImport PageBuilderService pageBuilderService) {
??????? this.pageBuilderService = pageBuilderService;
??? }
?? ?public String execute(Map<String, String> map, String s, ConversionContext conversionContext) throws MacroExecutionException {
?? ??? ?pageBuilderService.assembler().resources().requireWebResource("com.atlassian.tutorial.myConfluenceMacro:myConfluenceMacro-resources");
?? ??? ?String output = "<div id=\"totalNotificationsBadge\"></div>";
??????? return output;
??? }

??? public BodyType getBodyType() { return BodyType.NONE; }

??? public OutputType getOutputType() { return OutputType.BLOCK; }
}
```

Data.txt

```
{"Success":"true","Message":"ok","totalNotifications":10}
```

When I call the function outside confuence it works fine. But when I use it inside confluence plugin it does not show the number (10)

Your kind assistance is highly appreicated.


Answers:
author: Daniel Wester
body: Don't redefine AJS.toInit... Things will get broken on the page. It's a function so you can just do

```
AJS.toInit(function() {?????????????? ?
?? ??? ?$.ajax({
?? ??? ??? ?url : 'data.txt',?? ?
?? ??? ?}).done(function(data){
?? ??? ??? ?// $('#newLessonsBadge').html(data);
?? ??? ??? ?? var obj = data;
?? ??? ??? ?? if (obj.Success == 'true'){
?? ??? ??? ?? $('#totalNotificationsBadge').html(obj.totalNotifications);
?? ??? ??? ?? }
?? ??? ??? ?? else{
?? ??? ??? ?? //$('#feedbackReject').html('<div class="col-lg-12" style="width:100%;margin:0;padding:0"><div class="alert alert-dismissible alert-danger"><button type="button" class="close" data-dismiss="alert">&times;</button><strong>Error!</strong> '+obj.Message+' Data Was not Saved</div></div>');
?? ??? ??? ?? }
?? ??? ?}).fail(function(){

?? ??? ?});
?? ?}); 
```

and it should get triggered properly.

You might want to ask your question over at the Developer Community if you continue to run into issues: <https://community.developers.atlassian.com> .

comments: author: Mustafa Abusalah
body: Hi Daniel,

Still it is not showing the div value. I can see the value passed successfully to the page:

![Capture.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/13292i51A8D883842E9599/image-size/large?v=v2&px=999 "Capture.PNG")


author: Domenico Manzo _Actonic_
body: What is it:

```
?url : 'data.txt',?? 
```

Is it your correct url?!

comments: author: Mustafa Abusalah
body: The original is a server script. When returns the result correctly in json format.


author: Domenico Manzo _Actonic_
body: Could you provide the full URL?


author: Mustafa Abusalah
body: http://localhost:8080/data.cfm


author: Domenico Manzo _Actonic_
body: Does your Confluence have same scheme "http" ? Or "https" ?


author: Mustafa Abusalah
body: Sure both servers are on my local machine on http not https. and the data is sent to confluence, I tracked it.


Tags: developer