Skip to main content
Level 5
Oct 27, 2020 1:46 AM UTC
Solved

JSON on page load

Hello Community - I have a property that is having a JSON value in the page's jcr:content. Basically, I wanted to load the JSON on the page load. Can someone provide the best approach to load the JSON?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Sandeep6Accepted solution
Level 4
Oct 27, 2020 5:09 AM UTC

Hi @v1101 ,

 

you can write a servlet and in that you can call a particular node using resource api.

Once you got the node as a resource, it is very easy to read particular property of that node using value map.

Now you can call the servlet on page load using jquery.

Please see below for sample code.

 

Resource resource = resolver.getResource("/content/sita/en_UK/x/y/jcr:content/"); 

$.ajax({
type: 'GET',
url:'/bin/sitea/servleta',
processData: false,
contentType: false,
data:formData,
success: function(msg){
alert("json");

}
});
}

 

Thanks,

Sandeep.

 

3 replies

Ravi_Pampana
Community Advisor
Community Advisor
Oct 27, 2020 3:30 AM UTC

Hi,

 

We can use Java-Use api or Javascript-use api to read json from the node and display in component html.

 

You can refer http://aemcorner.blogspot.com/2015/09/render-json-data-using-sightly-custom.html for example.

Translate
Ravi Pampana
v1101Author
Level 5
Oct 27, 2020 3:43 AM UTC
@ravi_pampana - The JSON property is available in jcrcontent node of the page and it is not in any of the component's node.
Translate
Sandeep6Accepted solution
Level 4
Oct 27, 2020 5:09 AM UTC

Hi @v1101 ,

 

you can write a servlet and in that you can call a particular node using resource api.

Once you got the node as a resource, it is very easy to read particular property of that node using value map.

Now you can call the servlet on page load using jquery.

Please see below for sample code.

 

Resource resource = resolver.getResource("/content/sita/en_UK/x/y/jcr:content/"); 

$.ajax({
type: 'GET',
url:'/bin/sitea/servleta',
processData: false,
contentType: false,
data:formData,
success: function(msg){
alert("json");

}
});
}

 

Thanks,

Sandeep.

 

Translate