Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

EXTJS need to parse response from AEM servlet and How to do?

Avatar

Former Community Member

There is a case to Add a button into sidekick, which need to be disable(grey) or enable also, controlled by one condition that data stored in AEM, please see following code:

CQ.Ext.ns("MyClientLib"); var resultFromAEM; MyClientLib.ContentFinder = { addGsTranslate: function(sk){ var pagePanel = sk.panels["PAGE"]; var button = pagePanel.findBy(function(comp){ return comp["name"] == "Test_button"; }, pagePanel); if(button && button.length > 0){ return; } button = { xtype: "button", scope: sk, name: "Test_button", text: "Test Button", "context": [ CQ.wcm.Sidekick.PAGE ], handler: function(){ //... }, //TODO :  “isable” controlled by data if existed in AEM var isable = resultFromAEM; disabled:(isable) }; pagePanel.insert(11,button); sk.actns.push(button); }, }; (function(){ var result; CQ.Ext.Ajax.request({ url: "/bin/customer", method: "GET", params : { }, success: function(response) { //TODO : TO invoke servlet but how to parse response from AEM Servlet var info = Ext.decode(response.responseText);//crash here!!!!             alert(info.message); }, failure: function(response) { } }); var c = MyClientLib.ContentFinder; if( ( window.location.pathname == "/cf" ) || ( window.location.pathname.indexOf("/content") == 0)){ var SK_INTERVAL = setInterval(function(){ var sk = CQ.WCM.getSidekick(); if(sk){ clearInterval(SK_INTERVAL); c.addGsTranslate(sk); } }, 250); } })();

Servlet code:

@SlingServlet(paths = "/bin/customer", methods = "POST", metatype = true) public class CustomerServlet extends SlingAllMethodsServlet { private static final long serialVersionUID = 2598426539166789515L; protected final Logger log = LoggerFactory.getLogger(this.getClass()); @Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServerException, IOException { int result = this.getCustomerService().hasAccessToken(); JSONWriter jw = new JSONWriter(response.getWriter());                 try {                     jw.array();                     jw.object();                     jw.key("result:").value(result);                     jw.endObject();                     jw.endArray();                 } catch (JSONException e) {                     log.error("JSON Exception:", e);             } } private CustomerService getCustomerService() { BundleContext ctx = FrameworkUtil.getBundle(this.getClass()) .getBundleContext(); ServiceReference serviceReference = ctx .getServiceReference(CustomerService.class.getName()); return CustomerService.class.cast(ctx.getService(serviceReference)); } }

Anyone could help see it? Thanks a lot.

1 Accepted Solution

Avatar

Correct answer by
Level 10

See this article to learn how to invoke a sling servlet from an xtype and work with the return data.

http://helpx.adobe.com/experience-manager/using/creating-custom-cq-tree.html

View solution in original post

7 Replies

Avatar

Correct answer by
Level 10

See this article to learn how to invoke a sling servlet from an xtype and work with the return data.

http://helpx.adobe.com/experience-manager/using/creating-custom-cq-tree.html

Avatar

Former Community Member

Hi scott, i change my servlet code following that article way. Like you know the xtype encapsulate it's field to JSON pairs key and value. And i just get a pair of key value from servlet and try to use this code "

 var info = Ext.decode(response.responseText); " to get return pair, but crash at the line. Do you have any solution for that? Thanks a lot. 

Avatar

Level 10

The important thing about this article was to show you HOW TO invoke a servlet from an xtype. You can modify the servlet to process data in a different way and encode the return data to address your business requirements. You can return JSON, XML, key/values, etc.   

Avatar

Level 10

Are you trying to encode the data so it will populate the Dynatree control? 

Avatar

Former Community Member

This case not a dynatree control, and just only get a variable value from servlet you can see the code in it:

jw.array(); jw.object(); jw.key("result:").value(result); jw.endObject(); jw.endArray();

Then the client to get this return like below:

var info = Ext.decode(response.responseText);

But when process run this line crash, i don't know how to deal with it.

Avatar

Level 10

What does the error message state?

Avatar

Former Community Member

Right now i try to change the client JS like below, It hint undefined.

var info = response.responseText; alert(info);  // [{"result":1}] alert(info.result); // undefined

And the servlet code is following:

jw.array(); jw.object(); jw.key("result").value(result); jw.endObject(); jw.endArray();