How to call a servlet from react component | Community
Skip to main content
March 8, 2022

How to call a servlet from react component

  • March 8, 2022
  • 3 replies
  • 8090 views

 

 

Hi,

do you have any tutorial on how to create servlet and call it from react?

 

I did this, I create one servlet like that

and I call it in react like that

 

and I got this error in console

 

 

How can I solve it?

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

3 replies

Siva_Sogalapalli
Community Advisor
Community Advisor
March 8, 2022

I hope you're trying to call the servlet from AEM itself, did you try by removing localhost:4502 and just use something like post('/bin/login', fromData) and see how it works.

 

 

 

March 8, 2022
Anmol_Bhardwaj
Community Advisor
Community Advisor
March 8, 2022

Can you try this and see if it works:

If your requests are getting blocked, this should fix it.

 

- Please check if you have implemented SlingHttpSafeServlet or AllMethodServlet in your servlet.

- Try to call the same servlet through Postman and see if it is working fine

 

March 8, 2022

@anmol_bhardwaj 

 

I tried that, does not works

 

I have this as filter

 

Anmol_Bhardwaj
Community Advisor
Community Advisor
March 8, 2022

So you're getting the same error in Postman, after allowing empty in the Sling?
The filter code looks good, but what about the servlet code? Is it implementing SlingAllMethodServlet?

 

Also, if the request doesn't get blocked, even if the servlet doesn't have a doPost method, it is not blocked, rather it just does nothing and sends 200.

 

So, I'm guessing the issue lies with blocking of the request.

Try to first send the request and get a 200 through Postman. This should help you get better clarity on the underlying issue.

Flores-Lucas
June 20, 2023

Just dealing with the same error here, I came around this article that may be helpful: