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 .
Solved! Go to Solution.
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
3. The path of the resource from the point #2 should be requested via ajax:
$.ajax({
url: '/content/trigger-servlet',
type: 'GET'
...
Regards
Yes you can call. There is no difference in the way to hit the Servlet(path or resource type) from client side.
@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'
...
Views
Replies
Total Likes
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.
$.ajax({
url: '/content/en/us/test/_jcr_content/par/tagcomponent',
type: 'GET'
...
Views
Replies
Total Likes
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
3. The path of the resource from the point #2 should be requested via ajax:
$.ajax({
url: '/content/trigger-servlet',
type: 'GET'
...
Regards
Views
Replies
Total Likes
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"
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.
Views
Likes
Replies
Views
Likes
Replies
Views
Like
Replies