Pickup environment specific configuration for usage in post call via ajax | Community
Skip to main content
cquser1
Level 7
July 28, 2017
Solved

Pickup environment specific configuration for usage in post call via ajax

  • July 28, 2017
  • 2 replies
  • 1959 views

Hi All,

Suppose I want to make a post call via AJAX, from my component which takes in certain details from the user and passes it to a backend servlet hosted on some external server.

A sample snippet something  like

$.ajax({

            url: url,

            type:"POST",

           success :

.................}}

the url will be something like http://example.abc//servlet?abc=xyz

The value/probably domain for example "example.abc" will be different for different environments[dev, prod etc], which will be stored in a location say /apps/example/config.prod in the xml files[in codebase].

So, how do I pickup the corresponding environment related values and place it to form the correct url, while passing from ajax call.

Any thoughts/pointers/reference code/snippet will be helpful.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by susheel

You need to include the Jquery in your component itself and dynamically pass the value to the jquery

You need the write some back end service WcmUsePojo class to read the config.

if you already have service where configuration is already read. You can use that service here.

I hope you are taking about run modes, which will load config appropriate to the environment based on the mode it is started in.

import java.io.IOException;

import org.osgi.service.cm.Configuration;

import org.osgi.service.cm.ConfigurationAdmin;

import com.adobe.cq.sightly.WCMUse;

public class ConfigurationPojo extends WCMUsePojo {

private String url;

public String getUrl() {

return url;

}

@Override

public void activate() {

ConfigurationAdmin configAdmin = getSlingScriptHelper().getService(ConfigurationAdmin.class);

try {

Configuration config = configAdmin.getConfiguration("com.adobe.cq.commerce.impl.asset.DynamicImageHandler");

if (null != config.getProperties() && null != config.getProperties().get("cq.commerce.asset.handler.name")) {

url = config.getProperties().get("cq.commerce.asset.handler.name").toString();

}

} catch (IOException e) {

System.out.println(e.getMessage());

}

}

}

This can be read in the sighly component and passed to Jquery in the component where ajax call is done.

<sly data-sly-use.config="ConfigurationPojo"/>

<script>

$.ajax({

url: ${config.url},

type:"POST",

...

}}

</script>

Reference Link Adobe CQ/Adobe AEM: How to work with Configurations in CQ

You will find many links related to reading configurations.

2 replies

susheel
susheelAccepted solution
Level 5
July 28, 2017

You need to include the Jquery in your component itself and dynamically pass the value to the jquery

You need the write some back end service WcmUsePojo class to read the config.

if you already have service where configuration is already read. You can use that service here.

I hope you are taking about run modes, which will load config appropriate to the environment based on the mode it is started in.

import java.io.IOException;

import org.osgi.service.cm.Configuration;

import org.osgi.service.cm.ConfigurationAdmin;

import com.adobe.cq.sightly.WCMUse;

public class ConfigurationPojo extends WCMUsePojo {

private String url;

public String getUrl() {

return url;

}

@Override

public void activate() {

ConfigurationAdmin configAdmin = getSlingScriptHelper().getService(ConfigurationAdmin.class);

try {

Configuration config = configAdmin.getConfiguration("com.adobe.cq.commerce.impl.asset.DynamicImageHandler");

if (null != config.getProperties() && null != config.getProperties().get("cq.commerce.asset.handler.name")) {

url = config.getProperties().get("cq.commerce.asset.handler.name").toString();

}

} catch (IOException e) {

System.out.println(e.getMessage());

}

}

}

This can be read in the sighly component and passed to Jquery in the component where ajax call is done.

<sly data-sly-use.config="ConfigurationPojo"/>

<script>

$.ajax({

url: ${config.url},

type:"POST",

...

}}

</script>

Reference Link Adobe CQ/Adobe AEM: How to work with Configurations in CQ

You will find many links related to reading configurations.

joerghoh
Adobe Employee
Adobe Employee
July 28, 2017

I don't think it's a good way to have different values per environment, because it's really hard to test PROD values in UAT.

I would the frontend pass always the same value and translate it on serverside to their respectice values (e.g. based on runmodes).

kind regards,
Jörg