Exposing a Commerce cloud JS Module as WebService #532
Replies: 8 comments
-
If you mean exposing it is a REST service, like ocapi then the answer is
no. As of now we can not create our cuatom ocapi.
Having said that, you can always expose it as a http resource, which
returns json instead of html.
Since this service will be internally consumed and should not change very
often, you can create this json returning resource(exposed via controller).
…On Wed, Apr 10, 2019, 5:53 PM Amine Gabriel Bennani < ***@***.***> wrote:
Hello guys.
In one hand I have a JS Back-End module that returns a dynamic JSON file.
It's present is a specific cartridge.
In the other hand I have a integrated SPA that should call this module to
retrieve the JSON in initialization phase.
Is it possible to expose this module as a WebService in Commerce Cloud?
if it's possible can you please tell to do this?
Thank you in advance for your answers.
Best regards.
Amine G Bennani
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<https://github.com/orgs/SalesforceCommerceCloud/teams/community/discussions/20>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/Aogm5XWZbBuregFLoxWSjRGkorUfo2ZTks5vfdfYgaJpZM4cm3RD>
.
|
Beta Was this translation helpful? Give feedback.
-
Hi, Thank you for your response. Can you please guide me on exposing the module as a http resource? Thank you. Best regards. Amine |
Beta Was this translation helpful? Give feedback.
-
Hi Amine
You can create a normal controller and export it using get request.
In that controller you can have your logic to build up the json and use
res.json to render the json.
So when you hit the controller url through browser it will be a get request
which will render your json
Let me know if you need more help.
On Wed, 10 Apr 2019 at 8:47 PM, Amine Gabriel Bennani < ***@***.***> wrote:
Hi,
Thank you for your response.
Can you please guide me on exposing the module as a http resource?
Thank you.
Best regards.
Amine
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<https://github.com/orgs/SalesforceCommerceCloud/teams/community/discussions/20/comments/2>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ANi2XxgX_IfI1pl4hhmV8iv0YVSsmqK-ks5vfd1ugaJpZM4cm3RD>
.
--
Thanks & Regards,
Nimit Shukla
Mobile: +65 84575208
|
Beta Was this translation helpful? Give feedback.
-
Hello Nimit, What a pleasure to run into you on this wonderfull forum 👍 Thank you for your reply I'll run some tests on it. Best regards. Amine G Bennani |
Beta Was this translation helpful? Give feedback.
-
This is standard cross site scripting error, which is standrd way of
browsers for implementing security...
Nothing sfcc specific, i would recommend you do googling, you are making
ajax call from different domain...
Search this error and check the cause and suggestions
…On Wed, Apr 10, 2019, 7:57 PM Amine Gabriel Bennani < ***@***.***> wrote:
Hello,
I've created a controller using server.get function and exported it
Ones the SPA developper hits the controller URL he receives the following
error message:
Access to XMLHttpRequest at '
https://dev08-eu01-longchamp.demandware.net/on/demandware.store/Sites-Longchamp-Site/fr_FR/getOptions-GetOptions'
from origin 'http://localhost:4200' has been blocked by CORS policy: No
'Access-Control-Allow-Origin' header is present on the requested resource.
How can I allow the SPA to access to my URL?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://github.com/orgs/SalesforceCommerceCloud/teams/community/discussions/20/comments/5>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/Aogm5ZR8olg3Z_owiopVvCcU1QPc5Ye-ks5vffTKgaJpZM4cm3RD>
.
|
Beta Was this translation helpful? Give feedback.
-
@Gabenny Please see this article here for helpful information on the Access-Control-Allow-Origin response header: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin The bottom line is that the getOptions-GetOptions resource needs to add the Access-Control-Allow-Origin header to the response, similar to:
To be safe, it is generally recommended to maintain a list of allowed origins (In SFCC, custom preference or JSON config would be simplest) and compare the request origin against the list. If a match is found, you can use the request origin when setting the response header. Psuedo code:
|
Beta Was this translation helpful? Give feedback.
-
Hi Amine
i added the following code
res.setHttpHeader(dw.system.Response.ACCESS_CONTROL_ALLOW_ORIGIN, '*');
and got the response from the SFCC exposed url even when i call it from
localhost.
the issue you are facing is due to your site is protected and it requires
an authentication to show up the storefront.
i recommend you to change your site status to online instead of online
protected and it will work.
Thanks & Regards
Nimit SHukla
…On Thu, Apr 11, 2019 at 3:13 PM Amine Gabriel Bennani < ***@***.***> wrote:
Hi,
Thank you for your reply.
I've tried the folowing code:
var stringifiedOject = JSON.stringify(jsonObject);
response.setHttpHeader(dw.system.Response.ACCESS_CONTROL_ALLOW_ORIGIN,
'*');
response.setHttpHeader(dw.system.Response.ACCESS_CONTROL_ALLOW_METHODS,
"POST, GET, OPTIONS");
response.setContentType('application/json');
response.getWriter().write(stringifiedOject);
but still the SPA return the same error.
Can you please tell me if there is something wrong with my code?
Thank you.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://github.com/orgs/SalesforceCommerceCloud/teams/community/discussions/20/comments/8>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ANi2X1Yw6bZb8VSIrOA05hpwsW-smwITks5vfuCdgaJpZM4cm3RD>
.
--
Thanks & Regards,
Nimit Shukla
Mobile: +65 84575208
|
Beta Was this translation helpful? Give feedback.
-
Awesome, this should get the job done. Just a word of caution, Nimit's code
is just an example, you should replace * with the known/trusted domains,
else it will allow any domain to access the resource.
…On Fri, Apr 12, 2019, 8:22 AM Nimit Shukla ***@***.***> wrote:
Hi Amine
i added the following code
res.setHttpHeader(dw.system.Response.ACCESS_CONTROL_ALLOW_ORIGIN, '*');
and got the response from the SFCC exposed url even when i call it from
localhost.
the issue you are facing is due to your site is protected and it requires
an authentication to show up the storefront.
i recommend you to change your site status to online instead of online
protected and it will work.
Thanks & Regards
Nimit SHukla
On Thu, Apr 11, 2019 at 3:13 PM Amine Gabriel Bennani <
***@***.***> wrote:
> Hi,
> Thank you for your reply.
> I've tried the folowing code:
> var stringifiedOject = JSON.stringify(jsonObject);
> response.setHttpHeader(dw.system.Response.ACCESS_CONTROL_ALLOW_ORIGIN,
> '*');
> response.setHttpHeader(dw.system.Response.ACCESS_CONTROL_ALLOW_METHODS,
> "POST, GET, OPTIONS");
> response.setContentType('application/json');
> response.getWriter().write(stringifiedOject);
>
> but still the SPA return the same error.
>
> Can you please tell me if there is something wrong with my code?
>
> Thank you.
>
> —
> You are receiving this because you commented.
> Reply to this email directly, view it on GitHub
> <
https://github.com/orgs/SalesforceCommerceCloud/teams/community/discussions/20/comments/8
>,
> or mute the thread
> <
https://github.com/notifications/unsubscribe-auth/ANi2X1Yw6bZb8VSIrOA05hpwsW-smwITks5vfuCdgaJpZM4cm3RD
>
> .
>
--
Thanks & Regards,
Nimit Shukla
Mobile: +65 84575208
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://github.com/orgs/SalesforceCommerceCloud/teams/community/discussions/20/comments/9>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/Aogm5Zf58K0VLr3U3SzclAtN9m9RJo_mks5vf_TugaJpZM4cm3RD>
.
|
Beta Was this translation helpful? Give feedback.
-
Hello guys.
In one hand I have a JS Back-End module that returns a dynamic JSON file.
It's present is a specific cartridge.
In the other hand I have a integrated SPA that should call this module to retrieve the JSON in initialization phase.
Is it possible to expose this module as a WebService in Commerce Cloud?
if it's possible can you please tell to do this?
Thank you in advance for your answers.
Best regards.
Amine G Bennani
Beta Was this translation helpful? Give feedback.
All reactions