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

Ajax call url for default resource type

Avatar

Avatar
Boost 5
Level 2
keerthana_hn
Level 2

Likes

6 likes

Total Posts

20 posts

Correct Reply

0 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
Validate 1
View profile

Avatar
Boost 5
Level 2
keerthana_hn
Level 2

Likes

6 likes

Total Posts

20 posts

Correct Reply

0 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
Validate 1
View profile
keerthana_hn
Level 2

11-05-2021

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?

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Validate 1
MVP
Umesh_Thakur
MVP

Likes

147 likes

Total Posts

157 posts

Correct Reply

53 solutions
Top badges earned
Validate 1
Applaud 25
Ignite 3
Ignite 1
Give Back 5
View profile

Avatar
Validate 1
MVP
Umesh_Thakur
MVP

Likes

147 likes

Total Posts

157 posts

Correct Reply

53 solutions
Top badges earned
Validate 1
Applaud 25
Ignite 3
Ignite 1
Give Back 5
View profile
Umesh_Thakur
MVP

13-05-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;

 

@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

 

 

Answers (5)

Answers (5)

Avatar

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

613 likes

Total Posts

590 posts

Correct Reply

231 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

613 likes

Total Posts

590 posts

Correct Reply

231 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
BrianKasingli
MVP

11-05-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?

Avatar

Avatar
Boost 5
Level 2
keerthana_hn
Level 2

Likes

6 likes

Total Posts

20 posts

Correct Reply

0 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
Validate 1
View profile

Avatar
Boost 5
Level 2
keerthana_hn
Level 2

Likes

6 likes

Total Posts

20 posts

Correct Reply

0 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
Validate 1
View profile
keerthana_hn
Level 2

11-05-2021

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

Avatar

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

613 likes

Total Posts

590 posts

Correct Reply

231 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

613 likes

Total Posts

590 posts

Correct Reply

231 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
BrianKasingli
MVP

11-05-2021

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"})

Avatar

Avatar
Boost 5
Level 2
keerthana_hn
Level 2

Likes

6 likes

Total Posts

20 posts

Correct Reply

0 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
Validate 1
View profile

Avatar
Boost 5
Level 2
keerthana_hn
Level 2

Likes

6 likes

Total Posts

20 posts

Correct Reply

0 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
Validate 1
View profile
keerthana_hn
Level 2

11-05-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"})

 

Avatar

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

613 likes

Total Posts

590 posts

Correct Reply

231 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

613 likes

Total Posts

590 posts

Correct Reply

231 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
BrianKasingli
MVP

11-05-2021

Can you share with us your Sling Servlet annotations?