File not found when calling SlingServlet from AJAX or a url | Community
Skip to main content
Level 6
Oct 16, 2015 2:29 AM UTC
Solved

File not found when calling SlingServlet from AJAX or a url

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
kautuk_sahni
kautuk_sahniAccepted solution
Employee
Oct 16, 2015 2:29 AM UTC

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

5 replies

New Participant
Mar 16, 2020 5:33 PM UTC

Muje 9991678305 delial nikalni hai

 

Translate
New Participant
Jan 4, 2020 2:46 AM UTC

Hello 143

Translate
Level 3
Oct 16, 2015 2:29 AM UTC

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.

Translate
kautuk_sahni
kautuk_sahniAccepted solution
Employee
Oct 16, 2015 2:29 AM UTC

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

Translate
Kautuk Sahni
Level 6
Oct 16, 2015 2:29 AM UTC

Thank you, this worked for me.

Translate