Expand my Community achievements bar.

SOLVED

can we do ajax call of a servlet registered with resourceType?

Avatar

Level 7

Very classic question bt not clear to me ..can we do ajax call of a servlet registered with resourceType?

 

I have read some article they talk about calling the page (which has that particular resourceType component )with selector( mentioned during servlet registration )will automatically trigger the servlet registered with that resource Type .

 

but wanted to know in AJAX respect can we do ajax call as we do for servlet registered with Path ?

 

Any real time scenerio will be helpful .

1 Accepted Solution

Avatar

Correct answer by
Level 4

Hello @AdobeID24,

1. The servlet should be registered with the desired resource type:

@SlingServletResourceTypes(
resourceTypes = "/bin/target-resource-type",
methods = HttpConstants.METHOD_GET
)
public class SimpleServlet extends SlingAllMethodsServlet {

2. The resource with the corresponding resource type should be created, e.g. /content/trigger-servlet containing the property sling:resourceType with the value /bin/target-resource-type
Screenshot.png

 

3. The path of the resource from the point #2 should be requested via ajax:

$.ajax({
url: '/content/trigger-servlet',
type: 'GET'
...


Regards

View solution in original post

7 Replies

Avatar

Community Advisor

@AdobeID24 

Yes you can call. There is no difference in the way to hit the Servlet(path or resource type) from client side. 

Avatar

Level 7

@Anudeep_Garnepudi...Thanks for the reply ...can you please explain me little bit more it wiuld be helpful .

 

Suppose i regitser my servlet on reourceType:/apps/<project-name>/component/content/tagcomponent

 

Can we call like that?

 

 

$.ajax({
url: '/apps/<project-name>/component/content/tagcomponent',
type: 'GET'
...

 

Avatar

Community Advisor

@AdobeID24

You can not call using component(/apps/<project-name>/component/content/tagcomponent) path directly. You should drop the component in a page and you should hit that component path.

  1. Create a page (/content/en/us/test.html)
  2. Drop the Tag Component in page (/content/en/us/test.html)
  3. Use the component path as AJAX url (/content/en/us/test/_jcr_content/par/tagcomponent)
$.ajax({
    url: '/content/en/us/test/_jcr_content/par/tagcomponent',
    type: 'GET' 
    ...

Avatar

Correct answer by
Level 4

Hello @AdobeID24,

1. The servlet should be registered with the desired resource type:

@SlingServletResourceTypes(
resourceTypes = "/bin/target-resource-type",
methods = HttpConstants.METHOD_GET
)
public class SimpleServlet extends SlingAllMethodsServlet {

2. The resource with the corresponding resource type should be created, e.g. /content/trigger-servlet containing the property sling:resourceType with the value /bin/target-resource-type
Screenshot.png

 

3. The path of the resource from the point #2 should be requested via ajax:

$.ajax({
url: '/content/trigger-servlet',
type: 'GET'
...


Regards

Avatar

Administrator
Very well written reply. Thank you for sharing this with community.


Kautuk Sahni

Avatar

Employee Advisor

@AdobeID24 

AFAIK when a servlet is registered with 'resourceType', it will be called if the respective component is invoked and servlet cannot be requested through ajax. If there's a need to call the same servlet with ajax too, then register it with 'path' as well.

 

As per the sling documentation, if you would specify both 'resourceType' and 'path' in the servlet, it would be registered both ways.

https://sling.apache.org/documentation/the-sling-engine/servlets.html

"If both are set, the servlet is registered using both ways"

Avatar

Employee Advisor

@AdobeID24 

I tried with the json extension (rather than html as I was referring earlier) in ajax as below:

@SlingServlet(resourceTypes = "<your resurceType>", selectors = "myselector",

extensions = "json", methods = "GET")

$.ajax({ type:"GET", url: "<resourcepath>/_jcr_content.myselector.json", ...});

If you're using 'page' as the resourcetype, you could replace your resourcetype in the servlet with "cq/page" and would not have to use ''_jcr_content" in the ajax url/path.