Confluence Plugin Development Javascript

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.

 

2 answers

This widget could not be displayed.

What is it:

 url : 'data.txt',   

 Is it your correct url?!

The original is a server script. When returns the result correctly in json format.

Could you provide the full URL?

http://localhost:8080/data.cfm

Does your Confluence have same scheme "http" ? Or "https" ?

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

This widget could not be displayed.
Daniel Wester Community Champion Jan 24, 2018

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 .

Hi Daniel,

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

Capture.PNG

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Monday in Confluence

Why start from scratch? Introducing four new templates for Confluence Cloud

Hi my Community friends!  For those who don't know me, I'm a product marketer on the Confluence Cloud team - nice to meet you! For those of you who do, you know that I've been all up in your Co...

477 views 6 6
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you