Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Pickup environment specific configuration for usage in post call via ajax

Avatar

Level 8

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.

1 Accepted Solution

Avatar

Correct answer by
Level 5

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.

View solution in original post

2 Replies

Avatar

Correct answer by
Level 5

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.

Avatar

Employee Advisor

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