# Cors policy error inside template node but not in http request node

**URL:** <https://discourse.nodered.org/t/cors-policy-error-inside-template-node-but-not-in-http-request-node/66797>\
**Category:** General\
**Created:** [23 August 2022 07:15 UTC](https://discourse.nodered.org/t/cors-policy-error-inside-template-node-but-not-in-http-request-node/66797 "2022-08-23T07:15:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![joshuaC](https://avatars.discourse-cdn.com/v4/letter/j/d6d6ee/32.png) [@joshuaC](https://discourse.nodered.org/u/joshuaC)\
**Post date:** [23 August 2022 07:15 UTC](https://discourse.nodered.org/t/cors-policy-error-inside-template-node-but-not-in-http-request-node/66797/1 "2022-08-23T07:15:15Z")

</div>

Hello,  
I have a template node that is trying to retrieve data with a post request (fetch), I have a CORS policy error that I don't manage to fix.

`Access to fetch at 'https://example-elasticsearch/index_3/_search' from origin 'https://mysite.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.`

this is my javascript (in the template node code) :

```auto
const userAction = async () => {
  console.info('in');
  const myBody = {"query": {"bool": {"must": [{ "match": { "My": 4000 }}]}}};
  const username = 'hidden';
  const password = 'hidden';
  const response = await fetch('https://example-elasticsearch/index_3/_search', {
    method: 'POST',
    body: myBody, // string or object
    headers: {
      'Content-Type': 'application/json',
      "Authorization": 'Basic ' + btoa(username + ":" + password)
    }
  });
  
const myJson = await response.json(); //extract JSON from the http response
console.info(myJson);
}

```

I tried to add this line in the header without any success :  
`'Access-Control-Allow-Origin': '*'`

In my opinion it's not a problem on the server where I retrieve the data because when I try to retrieve the data using an http request node it is working.

Does someone have any idea how I can fix that?

Thanks a lot

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [23 August 2022 07:26 UTC](https://discourse.nodered.org/t/cors-policy-error-inside-template-node-but-not-in-http-request-node/66797/2 "2022-08-23T07:26:57Z")

</div>

> [@joshuaC](#):
>
> In my opinion it's not a problem on the server where I retrieve the data because when I try to retrieve the data using an http request node it is working.
> 
> Does someone have any idea how I can fix that?

When the HTTP request operates, it is FROM your node-red server.

When the `fetch` operates, it is from the clients browser.

These are different.

NOTE: CORS control is set by the endpoint server not the requester

I can only assume `https://example-elasticsearch/index_3/_search` is on the same box as `node-red` and that the browser is on a separate machine?

---

<div class="post-metadata">

**Author:** ![joshuaC](https://avatars.discourse-cdn.com/v4/letter/j/d6d6ee/32.png) [@joshuaC](https://discourse.nodered.org/u/joshuaC)\
**Post date:** [23 August 2022 09:12 UTC](https://discourse.nodered.org/t/cors-policy-error-inside-template-node-but-not-in-http-request-node/66797/3 "2022-08-23T09:12:51Z")

</div>

I believe you are right, I guess that I need to change some configuration in the elastic server.  
Thanks for the help this is really helpful.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [6 September 2022 09:13 UTC](https://discourse.nodered.org/t/cors-policy-error-inside-template-node-but-not-in-http-request-node/66797/4 "2022-09-06T09:13:18Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
