File not found when calling SlingServlet from AJAX or a url | Community
Skip to main content
New Participant
October 16, 2015
Solved

File not found when calling SlingServlet from AJAX or a url

  • October 16, 2015
  • 5 replies
  • 5283 views

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.
Best answer by kautuk_sahni

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

5 replies

New Participant
March 16, 2020

Muje 9991678305 delial nikalni hai

 

New Participant
January 4, 2020

Hello 143

New Participant
October 16, 2015

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.

kautuk_sahni
kautuk_sahniAccepted solution
Employee
October 16, 2015

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
New Participant
October 16, 2015

Thank you, this worked for me.