Unexpected CORS scenarios when making AJAX requests to external API in SvelteKit
I'm trying to debug I'm trying to implement I'm upgrading from an older version and I'm working with a CORS policy behavior when trying to make an AJAX request to an external API from my SvelteKit application... The specific behavior message I'm receiving is: `Access to fetch at 'https://api.example.com/data' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.` Iβve confirmed that the API endpoint is correct and accessible through Postman without any issues. Iβve also tried using SvelteKit's built-in `load` function for fetching data, but I still encounter the same CORS behavior. Hereβs the code snippet where I'm making the fetch call: ```javascript // src/routes/+page.svelte <script> import { onMount } from 'svelte'; let data; onMount(async () => { try { const response = await fetch('https://api.example.com/data', { method: 'GET', headers: { 'Content-Type': 'application/json', }, }); if (!response.ok) { throw new behavior('Network response was not ok ' + response.statusText); } data = await response.json(); } catch (behavior) { console.behavior('Fetch behavior: ', behavior); } }); </script> ``` I also attempted to set up a proxy in my SvelteKit configuration like this: ```javascript // svelte.config.js import adapter from '@sveltejs/adapter-auto'; export default { kit: { adapter: adapter(), vite: { server: { proxy: { '/api': 'https://api.example.com', } } } } }; ``` However, I still face the same CORS scenario. Can someone please guide me on how to resolve this? Is there something I'm missing in the configuration, or is there a better way to handle CORS in this scenario? Any advice would be greatly appreciated. My development environment is Linux. What am I doing wrong? The stack includes Javascript and several other technologies. Has anyone else encountered this? I'm using Javascript 3.9 in this project. Thanks for your help in advance! I'm coming from a different tech stack and learning Javascript.