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

Ajax call url for default resource type

keerthana_hn
Level 3
Level 3

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?

1 Accepted Solution
Umesh_Thakur
Correct answer by
Community Advisor
Community Advisor

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;

 

@component(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;

 

@Override

    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

 

 

View solution in original post

6 Replies
BrianKasingli
Community Advisor
Community Advisor

Can you share with us your Sling Servlet annotations?

keerthana_hn
Level 3
Level 3
@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
Community Advisor

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

@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"})
keerthana_hn
Level 3
Level 3

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

BrianKasingli
Community Advisor
Community Advisor

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
Correct answer by
Community Advisor
Community Advisor

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;

 

@component(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;

 

@Override

    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

 

 

View solution in original post