function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Héron SilvaHéron Silva 

LWC request to external API CORS error

Hello guys,

I am currently working on the development of a Lightning Web Components, first time developing for Salesforce. I've already searched everywhere but I couldn't find a working solution in order to make requests to my company's server.

I've already added our server to the list of security trusted sites and I am having problems running the following:
fetch('https://example.com', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    data: { ... }
}).then(function(res) {
    console.log(res);
}).catch(e => console.log(e));
This is the error I get:
Access to fetch at 'https://example.com' from origin 'https://origin-dev-ed.lightning.force.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.

Our API is returning the required 'Access-Control-Allow-Origin' header and we shouldn't be seeing such error. Or am I missing something?
AnudeepAnudeep (Salesforce Developers) 
There is an ongoing issue. I recommend raising a support case if you have any specific questions

https://trailblazer.salesforce.com/issues_view?id=a1p4V000001JipTQAS&title=enable-secure-static-resources-for-lightning-components-causes-a-cors-issue-for-static-resources-loaded-from-an-aura-component

Let me know if this helps, if it does, please close the query by marking it as solved. It may help others in the community. Thank You!
Héron SilvaHéron Silva
Hello Anudeep,
Thanks for the reply.

It doesn't seem that the link you shared is related to the same problem I am facing.
I am currently trying to send the request from within a Lightning Web Component (via Javascript) to an external server (not a salesforce endpoint). The external server is from my company, it has the correct headers (Access-Control-Allow-Origin) to avoid CORS errors, and it has been used by other integrations we have developed so far (ex. Zendesk).

Is it possible to do such requests from within a Salesforce LWC?

Best regards,
Héron
プウィンプュ ニン 2プウィンプュ ニン 2
Hello Héron,
 Have you solved this problem?
 I am facing the same probem. Could you tell me the solution you used?
Best regards,
Hnin
AnudeepAnudeep (Salesforce Developers) 
I am curious about the mode in the code. Do you know the value in your case. Can you try setting it to no-cors and see if it makes any difference
 async function postData(url = '', data = {}) {
  // Default options are marked with *
  console.log(data)
  const response = await fetch(url, {
    method: 'POST', // *GET, POST, PUT, DELETE, etc.
    mode: 'cors', // no-cors, *cors, same-origin
    cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
    credentials: 'same-origin', // include, *same-origin, omit
    headers: {
      //'Content-Type': 'application/json'
       'Content-Type': 'application/x-www-form-urlencoded',
    },
    redirect: 'follow', // manual, *follow, error
    referrerPolicy: 'no-referrer', // no-referrer, *client
    body: JSON.stringify(data) // body data type must match "Content-Type" header
  });
  return await response.json(); // parses JSON response into native JavaScript objects
}

As per the documentation, the following salesforce technologies support CORS.

Analytics REST API
Bulk API
Connect REST API
Salesforce IoT REST API
Lightning Out
REST API
User Interface API 
Apex REST

I recommend raising a support case if you are still experiencing this issue
 
NamitTrailhead PandeNamitTrailhead Pande
Hello,
I am facing the Similar CORS issue when trying to hit external API from my LWC. Did you find any solution?.
Thanks in advance
GiuliaMGiuliaM
The issue went away for me only when I added "mode: 'no-cors'" to my callout but still I don't know if this is the way to go and also having added the endpoint to CORS and CSP from Setup from my understanding it should be working. I really don't get it.
Manikanta Allada 8Manikanta Allada 8

Hi,

 

From my experience, this is the error browser brings in and blocking the cross domain URLS. It's better to make the external API callfrom the apex and calling that class which is if same domain from the LWC component.

jesszi12 joejesszi12 joe
Great Post! Thank you for visiting here. https://nextgen-patientportal.com/