You need to sign in to do that
Don't have an account?
Hé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:
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?
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?
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!
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
Have you solved this problem?
I am facing the same probem. Could you tell me the solution you used?
Best regards,
Hnin
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
I am facing the Similar CORS issue when trying to hit external API from my LWC. Did you find any solution?.
Thanks in advance
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.