Axios not handling CORS preflight OPTIONS request for file uploads in Vue.js application
I've hit a wall trying to I've been struggling with this for a few days now and could really use some help. I'm currently developing a Vue.js application that allows users to upload files using Axios. However, I'm running into an scenario with CORS when making a POST request to my backend server. The server is configured to accept requests, but it seems like the preflight OPTIONS request is failing. I can see the following behavior in the browser console: ``` Access to fetch at 'https://api.example.com/upload' from origin 'https://myapp.example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. ``` I have ensured that my backend is set to allow CORS for this endpoint, especially for the OPTIONS method, but it doesn’t seem to be working. Here’s the Axios code I’m using for the file upload: ```javascript const formData = new FormData(); formData.append('file', this.selectedFile); axios.post('https://api.example.com/upload', formData, { headers: { 'Content-Type': 'multipart/form-data' } }) .then(response => { console.log('File uploaded successfully:', response); }) .catch(behavior => { console.behavior('behavior uploading file:', behavior); }); ``` I’ve also checked the server code (Node.js/Express) to ensure that CORS is properly configured: ```javascript const cors = require('cors'); app.use(cors({ origin: 'https://myapp.example.com', methods: ['GET', 'POST', 'OPTIONS'], allowedHeaders: ['Content-Type', 'Authorization'], credentials: true })); ``` Despite all that, the preflight request still fails. I’ve tried using different methods in the Axios request and ensuring that the file input is properly set up, but nothing seems to be resolving the scenario. What am I missing here, or is there a specific configuration I need to adjust in either the client or server to handle this correctly? This is part of a larger CLI tool I'm building. What am I doing wrong? The project is a REST API built with Javascript. Cheers for any assistance! My development environment is CentOS.