Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Invoke API to set browser cookies

Avatar

Level 5

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?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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-trackin...

 

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

View solution in original post

3 Replies

Avatar

Community Advisor

Hi @srikanthpogula ,

 

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 

Avatar

Level 5

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

Avatar

Correct answer by
Community Advisor

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-trackin...

 

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