File not found when calling SlingServlet from AJAX or a url

Clive_Stewart

15-10-2015

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

Accepted Solutions (1)

Accepted Solutions (1)

kautuk_sahni

Community Manager

15-10-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

Answers (4)

Answers (4)

awadheshv

15-10-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.