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">×</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.
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">×</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:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
What is it:
url : 'data.txt',
Is it your correct url?!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The original is a server script. When returns the result correctly in json format.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Could you provide the full URL?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Does your Confluence have same scheme "http" ? Or "https" ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Sure both servers are on my local machine on http not https. and the data is sent to confluence, I tracked it.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.