Expand my Community achievements bar.

SOLVED

File not found when calling SlingServlet from AJAX or a url

Avatar

Level 6

I recently migrated to AEM 6.1. In the process I had to reinstall some of my OSGI bundles.

 

They all worked except where I call them from AJAX.

I have a servlet that I register using

@SlingServlet(paths = "/bin/stores.json", methods = "POST", metatype = false)

Inside my class I have the following method

 

/////////////////////////

    @Override
    protected void doGet(SlingHttpServletRequest request,
            SlingHttpServletResponse response) throws ServerException,
            IOException {
        try {
            // http://localhost:4502/bin/stores.json?lat=-33.9168959&lng=18.418187&uid=1
            String storeUid = request.getParameter("uid");

 

//////////////////////////

I have checked that my osgi bundle is active on

http://localhost:4502/system/console/bundles.

 

When I call the following

http://localhost:4502/bin/stores.json?uid=44909

 

I get back

<html> <head> <title>File not found</title> </head> <body> <p>A custom errorhandler for 404 responses</p> </body> </html>

 

Do I have to define the path /bin/stores.json in one of the configuration interfaces on http://localhost:4502/system/console/configMgr

 

I looked at

Apache Sling Servlet / Script Resolver and Error handler with no success.

Please can anyone advice

Clive Stewart

1 Accepted Solution

Avatar

Correct answer by
Administrator

Hi Clive Stewart

As pointed correctly by Awadesh, you should either change method to “GET” or implement your logic in “doPost” method.

Please have a look at the article link: - https://helpx.adobe.com/experience-manager/using/custom-sling-servlets.html

-------------------------------

Brief about this article

-------------------------------

This article covers Submitting Adobe CQ form data to java Sling Servlets using POST method.

So Basically here we are doing AJAX POSTrequest :-

$.ajax({

         type: 'POST',   

         url:'/bin/mySearchServlet',

         data:'id='+ claimId+'&firstName='+ myFirst+'&lastName='+ myLast+'&address='+ address+'&cat='+ cat+'&state='+ state+'&details='+ details+'&date='+ date+'&city='+ city,

         success: function(msg){

           alert(msg); //display the data returned by the servlet

         }

     });

 

And OSGI java code is handling it by:-

protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServerException, IOException {     

<Code Logic>

}

 

I hope this will solve your problem.

 

Thanks and Regards

Kautuk Sahni



Kautuk Sahni

View solution in original post

5 Replies

Avatar

Level 3

you have defined your servlet to respond to POST requests but it looks like you are doing a GET. try adding GET to methods as following

methods = { "GET", "POST" }

if you still get error. look into error.log to find out what happened.

Avatar

Correct answer by
Administrator

Hi Clive Stewart

As pointed correctly by Awadesh, you should either change method to “GET” or implement your logic in “doPost” method.

Please have a look at the article link: - https://helpx.adobe.com/experience-manager/using/custom-sling-servlets.html

-------------------------------

Brief about this article

-------------------------------

This article covers Submitting Adobe CQ form data to java Sling Servlets using POST method.

So Basically here we are doing AJAX POSTrequest :-

$.ajax({

         type: 'POST',   

         url:'/bin/mySearchServlet',

         data:'id='+ claimId+'&firstName='+ myFirst+'&lastName='+ myLast+'&address='+ address+'&cat='+ cat+'&state='+ state+'&details='+ details+'&date='+ date+'&city='+ city,

         success: function(msg){

           alert(msg); //display the data returned by the servlet

         }

     });

 

And OSGI java code is handling it by:-

protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServerException, IOException {     

<Code Logic>

}

 

I hope this will solve your problem.

 

Thanks and Regards

Kautuk Sahni



Kautuk Sahni

Avatar

Level 1

Muje 9991678305 delial nikalni hai