Ajax call url for default resource type | Community
Skip to main content
Employee
May 11, 2021 4:55 PM UTC
Solved

Ajax call url for default resource type

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.
Umesh_Thakur
Umesh_ThakurAccepted solution
New Participant
May 13, 2021 12:58 PM UTC

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

Umesh_Thakur
Umesh_ThakurAccepted solution
New Participant
May 13, 2021 12:58 PM UTC

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

 

 

Translate
BrianKasingli
New Participant
May 11, 2021 11:31 PM UTC

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?

Translate
Employee
May 11, 2021 5:24 PM UTC

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

Translate
BrianKasingli
New Participant
May 11, 2021 5:21 PM UTC

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"})
Translate
Employee
May 11, 2021 5:18 PM UTC
@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"})

 

Translate
BrianKasingli
New Participant
May 11, 2021 5:16 PM UTC

Can you share with us your Sling Servlet annotations?

Translate