Expand my Community achievements bar.

SOLVED

404 response when making an XmlHttpRequest

Avatar

Level 2

I am making an raw ajax call on the clientside, which look like the following,

var url = '/bin/denisa/dummyServlet';
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
xhr.setRequestHeader('Accept', 'application/json, text/javascript, */*; q=0.01');
xhr.onload = function() {
   if (xhr.status === 200) {
   alert('deni');
  }
   else {
   alert('Request failed. Returned status of ' + xhr.status);
  }
};
xhr.send();

to the following servlet in AEM:

@Component(
   service = {
   Servlet.class
   },
   property = {
   "sling.servlet.paths=/bin/denisa/dummyServlet",
   "sling.servlet.extensions=json",
   "sling.servlet.methods=GET"
   }
)
public class DummyServlet extends SlingSafeMethodsServlet {

   private static final Logger LOG = LoggerFactory.getLogger(DummyServlet .class);

   @Reference
   private dummyService dummyService;

   @Override
   protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {

   Gson gson = new Gson();
   JsonElement jsonElement = gson.toJsonTree(dummyService);
   response.setContentType("application/json");
   response.getWriter().write(gson.toJson(jsonElement));
  }
}

And I got a 404, and i don't know exactly why because the path is the same, and also contentTypes. Does anybody has a clue?

1 Accepted Solution

Avatar

Correct answer by
Level 7

A few things to check:

- Is your bundle in active state.

- Is "dummyService" active and your servlet in active state.

- Can you try checking your servlet path under "http://localhost:4502/system/console/servletresolver "

View solution in original post

2 Replies

Avatar

Correct answer by
Level 7

A few things to check:

- Is your bundle in active state.

- Is "dummyService" active and your servlet in active state.

- Can you try checking your servlet path under "http://localhost:4502/system/console/servletresolver "

Avatar

Administrator

+1 with Vivek.

Also, have a look at this helpx article:- Adobe Experience Manager Help | Working with Sling Models in Adobe Experience Manager

Try to add the package directly and see if this works for you.

~kautuk



Kautuk Sahni