Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

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

AdobeID24
Level 6
Level 6

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
Andrei_Dantsou
Correct answer by
Level 4
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
Anudeep_Garnepudi
Community Advisor
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. 

AdobeID24
Level 6
Level 6

@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'
...

 

Anudeep_Garnepudi
Community Advisor
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' 
    ...
Andrei_Dantsou
Correct answer by
Level 4
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

kautuk_sahni
Community Manager
Community Manager
Very well written reply. Thank you for sharing this with community.
shelly-goel
Community Advisor
Community 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"

shelly-goel
Community Advisor
Community 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.