Forums

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

Confluence Plugin Development Javascript

Mustafa Abusalah December 30, 2017

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

0 votes
Daniel Wester
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 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 .

Mustafa Abusalah January 25, 2018

Hi Daniel,

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

Capture.PNG

0 votes
Domenico Manzo _Actonic_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 5, 2018

What is it:

 url : 'data.txt',   

 Is it your correct url?!

Mustafa Abusalah January 7, 2018

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

Domenico Manzo _Actonic_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 7, 2018

Could you provide the full URL?

Mustafa Abusalah January 7, 2018

http://localhost:8080/data.cfm

Domenico Manzo _Actonic_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 9, 2018

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

Mustafa Abusalah January 9, 2018

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events