How to call a servlet which is deployed inside AEM 6.2 from an application hosted on node server?

Avatar

Avatar
Validate 1
Level 1
sbg19
Level 1

Likes

0 likes

Total Posts

8 posts

Correct reply

0 solutions
Top badges earned
Validate 1
View profile

Avatar
Validate 1
Level 1
sbg19
Level 1

Likes

0 likes

Total Posts

8 posts

Correct reply

0 solutions
Top badges earned
Validate 1
View profile
sbg19
Level 1

30-12-2020

I have a servlet exposed through a path param 

@SlingServlet(paths = { "/exampleservlet" }, methods = {"GET"})

 

when I try to make a fetch call(GET) from my React Js front end layer hosted on node server, AEM is not allowing me to reach the servlet and I get a CORS policy exception in my browser console.

 

Any kind of assistance or suggestions would be appreciated.

Thank you.

 

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Validate 10
MVP
kunal23
MVP

Likes

166 likes

Total Posts

565 posts

Correct reply

172 solutions
Top badges earned
Validate 10
Validate 1
Ignite 3
Ignite 1
Give Back 50
View profile

Avatar
Validate 10
MVP
kunal23
MVP

Likes

166 likes

Total Posts

565 posts

Correct reply

172 solutions
Top badges earned
Validate 10
Validate 1
Ignite 3
Ignite 1
Give Back 50
View profile
kunal23
MVP

30-12-2020

Yes the request to servlet will be blocked as it is originated from an external application with a different domain. To make this work you will have to register the domain of the external application in AEM CORS OSGI configuration and bind it to the servlet path. Once you configure CORS then AEM will send appropriate response headers to the browser and the browser will be allowed to make the request. 

 

Also, you will have to enable caching of headers in dispatcher if you are caching the response of the servlet in the dispatcher. 

 

For more details please see these URLs- 

https://experienceleague.adobe.com/docs/experience-manager-learn/foundation/security/understand-cros...

https://experienceleague.adobe.com/docs/experience-manager-learn/foundation/security/develop-for-cro... 

 

 

Answers (2)

Answers (2)

Avatar

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

645 likes

Total Posts

606 posts

Correct reply

239 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

645 likes

Total Posts

606 posts

Correct reply

239 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
BrianKasingli
MVP

03-01-2021

@sbg19 during development, you would need to configure the Apache Sling Referrer Filter and Configure Adobe Granite CSRF Filter; you can review an example video of how to set it up here, https://sourcedcode.com/blog/aem/how-to-make-simple-http-post-methods-in-aem-with-a-http-rest-client

Avatar

Avatar
Affirm 100
MVP
shelly-goel
MVP

Likes

246 likes

Total Posts

410 posts

Correct reply

105 solutions
Top badges earned
Affirm 100
Give Back 25
Ignite 3
Give Back 10
Validate 1
View profile

Avatar
Affirm 100
MVP
shelly-goel
MVP

Likes

246 likes

Total Posts

410 posts

Correct reply

105 solutions
Top badges earned
Affirm 100
Give Back 25
Ignite 3
Give Back 10
Validate 1
View profile
shelly-goel
MVP

02-01-2021

@sbg19 

For older versions of AEM, you can either user jsonp response or set the CORS header in the servlet. You can find the details in below link and compare which one works or you.

https://www.albinsblog.com/2018/08/how-to-enable-cross-origin-resource-sharing-support-in-aem.html