Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Ajax call to retrieve date properties to be used on a component for a specific page type

Avatar

Level 9

Hi All,

Details as below :

There are two date properties, being used on a component which appears on a specific template. Now the requirement is to retrieve these values by making an AJAX call to some third party service. I have no clue as to how to go about this. 

Any thoughts/pointers on this will be really helpful. 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi ,

If your using sightly then you can use an use class where that class is called when the component is loaded i that use class make any custom osgi service and call the rest calls from that service and have properties stored in useclass.

You can also get all the page properties in that use class itself.

Thanks,

Mani Kumar K

View solution in original post

9 Replies

Avatar

Community Advisor

Hi 

You can refer the below link to know more about how to call ajax calls and retrieve data using sling servlets.

https://helpx.adobe.com/experience-manager/using/custom-sling-servlets.html

Mani Kumar 

Avatar

Level 9

Hi Mani,

Thank you for your reply.

@All,

With the data we have as of now, looks like we have to make this Ajax call to a restful service and pass a single parameter to it, based on which two date values will be provided which has to be made use of in our components. 

From the approach listed in  the above article

- Looks like we have to create a servlet, pass the required parameters and then obtain the results, which has to be displayed on the page, correct?

- Is there any other way we can achieve our objective?

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

Avatar

Level 10

In the servlet, write a query that retrieves page properties. Place prop values in  JSON data and return data.

Avatar

Community Advisor

Hi ,

you do not want to write a servlet ? if so please on load of the page where the component is present make an ajax call directly using jquery or javascript then display the response accordingly

please refer link to write ajax call on page load but instead making servlet call is best approach and handle all your rest calls in the servlet itself.

http://api.jquery.com/load/

--Mani Kumar K

Avatar

Level 9

Hi Mani,

Thank you for your reply.

We would like to go with the servlet approach. As you mentioned, we will call rest calls in the servlet itself. Now I am stuck with the below:

1] I have to fetch a page property and send it to the servlet.

2] We are using Sightly.

3] Based on Scott's article, trying the below in the sightly html file [not completely written yet]and when I run in debug mode, the control is going to servlet. However, in the below I am trying to fetch jcr:title property of the page to pass to customservlet and failing.

    <script>
    $(document).ready(function(){
    (function () {
    var title = granite.resource.properties["title"];    
});
    $.ajax({
         type: 'GET',    
         url:'/bin/customservlet',
         success: function(msg){
           var json = jQuery.parseJSON(msg); 
            var msgId=   json.id;
            var lastName = json.lastname;
            var firstName = json.firstname;
              
            $('#ClaimNum').val(msgId); 
            $('#json').val("Filed by " + firstName + " " + lastName);   
         }
     });
    })
    </script>

 

Any thoughts/pointers on this will be really helpful.

Avatar

Correct answer by
Community Advisor

Hi ,

If your using sightly then you can use an use class where that class is called when the component is loaded i that use class make any custom osgi service and call the rest calls from that service and have properties stored in useclass.

You can also get all the page properties in that use class itself.

Thanks,

Mani Kumar K

Avatar

Level 9

HI Mani,

Thank you for your reply.

A small change here. In the above AJax call to my servlet, I need not pass any property but just call the servlet and fetch the json data and display on the page.

For now, I am just trying the below (out of the entire thing to be done):

I tried to put this Ajax to servlet call in a seperate js file and trying to call that from sightly. Tried various options, but struggling here. Any snippet here would be helpful.

Avatar

Community Advisor

Can you share those snippets how your trying it ?