Ajax call url for default resource type | Community
Skip to main content
Adobe Employee
May 11, 2021
Solved

Ajax call url for default resource type

  • May 11, 2021
  • 6 replies
  • 2356 views

Hi all,

 

$(document).ready(function(){
$("#filesReportDownloadButton").click(function() {
var searchText=$("#cmp-fulltext_746019667").val();
$.ajax({
type: 'GET',
url: '${resource.path}.publishReport.html',
data: {
'SearchText':searchText
},
success: function (data) {
alert('ok');
},
error: function(jqXHR) {
alert('error');
}
});
});

 

I've written simple code to call my get servlet but not able to hit the servlet. What should be the url for default resource type with?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Umesh_Thakur

In a content page I have kept resourcetype as servletResourceType and firing ajax from browser's console the ajax was wroking fine

 

import javax.servlet.Servlet;

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.SlingHttpServletResponse;

import org.apache.sling.api.servlets.SlingSafeMethodsServlet;

import org.json.JSONObject;

import org.osgi.service.component.annotations.Component;

 

@8220494(name = "Resource Type ajax servlet", immediate = true, service = Servlet.class, property = {

"sling.servlet.resourceTypes=weretail/components/content", "sling.servlet.methods=GET" })

public class ResourceTypeAjaxDemoServlet extends SlingSafeMethodsServlet{

privatestaticfinallongserialVersionUID = 1L;

 

@9944223

    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)

    {

 

try {

String district = request.getParameter("dist").toString();

response.setContentType("application/json");

 

JSONObject jsonObject = new JSONObject();

jsonObject.put("Name", "Umesh Chandra Thakur");

jsonObject.put("Phone", "12334556");

jsonObject.put("email", "umesh@gmail.com");

jsonObject.put("City", "Khutauna");

jsonObject.put("dist", district);

response.getWriter().write(jsonObject.toString());

 

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

       

    }

 

 

}

/*

$.ajax({

    type : "GET",

    url : 'we-retail/language-masters/en_us/quick-links-test-page/jcr:content',

    data : {

        dist : 'Madhubani'

    },

    success : function(data, textStatus, jqXHR) {

        alert("successd",data);

                },

    error : function(XMLHttpRequest, textStatus, errorThrown) {

        //write your logic that you need to perform on error

    }

});

*/

 

 

Note siince jquery is not supported by browsers automatically so before running the ajax use below code in browser console to make jquery available for the browsers

 

(function(){
var newscript = document.createElement('script');
newscript.type = 'text/javascript';
newscript.async = true;
newscript.src='https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js';
(document.getElementsByTagName('head')[0]||document.getElementsByTagName('body')[0]).appendChild(newscript);
})();

 

hope this will help

Umesh Thakur

 

 

6 replies

BrianKasingli
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
May 11, 2021

Can you share with us your Sling Servlet annotations?

Adobe Employee
May 11, 2021
@Component(
service = Servlet.class,
property = {
ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES + "=" + ServletResolverConstants.DEFAULT_RESOURCE_TYPE,
ServletResolverConstants.SLING_SERVLET_METHODS + "=" + HttpConstants.METHOD_GET,
ServletResolverConstants.SLING_SERVLET_EXTENSIONS + "=" + "html",
ServletResolverConstants.SLING_SERVLET_SELECTORS + "=" + "fileReportReqResp"})

 

BrianKasingli
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
May 11, 2021

Your Ajax is calling the wrong selector, should be url: '${resource.path}.fileReportReqResp.html',

@8220494( service = Servlet.class, property = { ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES + "=" + ServletResolverConstants.DEFAULT_RESOURCE_TYPE, ServletResolverConstants.SLING_SERVLET_METHODS + "=" + HttpConstants.METHOD_GET, ServletResolverConstants.SLING_SERVLET_EXTENSIONS + "=" + "html", ServletResolverConstants.SLING_SERVLET_SELECTORS + "=" + "fileReportReqResp"})
Adobe Employee
May 11, 2021

Yes, I have added same selector still not able to hit servlet.

BrianKasingli
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
May 11, 2021

Can you try to visit your Servlet from the URL bar, within the browser?, since you are making a GET request?

Let's check if your OSGI service for your servlet is even enabled. Please head to OSGI console, click on "bundles" and then review your active services. Is the servlet there?

Umesh_Thakur
Community Advisor
Umesh_ThakurCommunity AdvisorAccepted solution
Community Advisor
May 13, 2021

In a content page I have kept resourcetype as servletResourceType and firing ajax from browser's console the ajax was wroking fine

 

import javax.servlet.Servlet;

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.SlingHttpServletResponse;

import org.apache.sling.api.servlets.SlingSafeMethodsServlet;

import org.json.JSONObject;

import org.osgi.service.component.annotations.Component;

 

@8220494(name = "Resource Type ajax servlet", immediate = true, service = Servlet.class, property = {

"sling.servlet.resourceTypes=weretail/components/content", "sling.servlet.methods=GET" })

public class ResourceTypeAjaxDemoServlet extends SlingSafeMethodsServlet{

privatestaticfinallongserialVersionUID = 1L;

 

@9944223

    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)

    {

 

try {

String district = request.getParameter("dist").toString();

response.setContentType("application/json");

 

JSONObject jsonObject = new JSONObject();

jsonObject.put("Name", "Umesh Chandra Thakur");

jsonObject.put("Phone", "12334556");

jsonObject.put("email", "umesh@gmail.com");

jsonObject.put("City", "Khutauna");

jsonObject.put("dist", district);

response.getWriter().write(jsonObject.toString());

 

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

       

    }

 

 

}

/*

$.ajax({

    type : "GET",

    url : 'we-retail/language-masters/en_us/quick-links-test-page/jcr:content',

    data : {

        dist : 'Madhubani'

    },

    success : function(data, textStatus, jqXHR) {

        alert("successd",data);

                },

    error : function(XMLHttpRequest, textStatus, errorThrown) {

        //write your logic that you need to perform on error

    }

});

*/

 

 

Note siince jquery is not supported by browsers automatically so before running the ajax use below code in browser console to make jquery available for the browsers

 

(function(){
var newscript = document.createElement('script');
newscript.type = 'text/javascript';
newscript.async = true;
newscript.src='https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js';
(document.getElementsByTagName('head')[0]||document.getElementsByTagName('body')[0]).appendChild(newscript);
})();

 

hope this will help

Umesh Thakur