Invoke API to set browser cookies | Community
Skip to main content
January 19, 2022
Solved

Invoke API to set browser cookies

  • January 19, 2022
  • 2 replies
  • 1576 views

Hi Team,

We have an third-party API when hit in browser address bar returns JSON response(session token) and creates few browser cookies which are used subsequently. We are trying to invoke this API via jQuery ajax call with dataType=jsonp, able to access JSON response but browser cookies are not created. As a result of this, subsequent functionality which is dependent on browser cookies is not working as expected.

Is there any way to solve this issue?

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 JeevanRaj

Hi Srikanth,

 

If your third party API is hosted in a different domain than your AEM then those cookies might not set onto your browser when you make an ajax from aem. Please look into the below articles which might give you more insight. Also chrome and safari are restricting third party cookies.

https://stackoverflow.com/questions/69448732/how-to-set-correct-cookies-in-express

https://stackoverflow.com/questions/46839903/third-party-cookies-cross-domain-apis-w-session-tracking

 

Consider writing a SlingServlet which will make the third party API call. Call this SlingServlet from your ajax. This might resolve your issue and also avoid browser CORS issue.

 

Thanks

2 replies

santhosh_kumark
January 19, 2022

Hi @srikanthpo3 ,

 

Try this below

$.ajax({
   url: a_cross_domain_url,
   xhrFields: {
      withCredentials: true
   }
});

Ref: https://stackoverflow.com/questions/2870371/why-is-jquerys-ajax-method-not-sending-my-session-cookie

 

Regards,

Santosh 

January 19, 2022

Hi @santhosh_kumark , the session toke API call itself creates browser cookies, its currently happening when API is hit in browser address bar. I am not trying to send cookies to API via ajax call. I am looking for way to invoke the API and get it to work in a similar way from AEM clientlibs, how it works when hit in browser address bar.

 

Regards,

Srikanth

JeevanRaj
Community Advisor
JeevanRajCommunity AdvisorAccepted solution
Community Advisor
January 19, 2022

Hi Srikanth,

 

If your third party API is hosted in a different domain than your AEM then those cookies might not set onto your browser when you make an ajax from aem. Please look into the below articles which might give you more insight. Also chrome and safari are restricting third party cookies.

https://stackoverflow.com/questions/69448732/how-to-set-correct-cookies-in-express

https://stackoverflow.com/questions/46839903/third-party-cookies-cross-domain-apis-w-session-tracking

 

Consider writing a SlingServlet which will make the third party API call. Call this SlingServlet from your ajax. This might resolve your issue and also avoid browser CORS issue.

 

Thanks