Issue with CORS Policy specifically in this scenario despite functioning properly for other assets

My API is built with Laravel and for the front-end, I am using Vue.js with Nuxt.js.

I have successfully installed the CORS policy package, which is working well with all my other resources. However, I encountered an error for one of my features:


        Access to XMLHttpRequest at 'http://myapp.test/api/bilan/storestatus' from origin 'http://localhost:3000'
        has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
        
        POST http://myapp.test/api/bilan/storestatus net::ERR_FAILED
    

Despite following the correct procedures, such as setting up the headers properly, the error still persists. Below is a snippet of my code:


        <el-switch v-model="status.status" @change="storeStatus">
        </el-switch>
    

        data() {
            status: { status: 0 }
        }
    

        async storeStatus() {
            try {
                await axios.post('bilan/storestatus', this.status)
                    .then( () => {
                        this.$message({
                            message: this.$t('status.updated'),
                            type: 'success'
                        })
                })
                .catch(error => {
                    console.log(error);
                })
            } catch (e) {
                console.log(e)
            }
        },
    

If you have any insights or suggestions regarding why this particular feature is causing issues while everything else is functioning correctly, please let me know. It's puzzling as to why this is happening.

Answer №1

Is there an internal error with your request? The CORS policy package in Laravel's default error handler can cause issues by creating new response headers. When the request goes through the error handler, it may remove the CORS headers, resulting in a CORS error instead of a 500 error for internal errors.

Answer №2

Make sure to activate CORS on your server as well. Additionally, include the line

axios.defaults.headers.post['Access-Control-Allow-Origin'] = '*';

If you encounter issues, it could be due to the server not accepting options requests. You can find more information about CORS at https://www.html5rocks.com/en/tutorials/cors/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a Future Prediction Graph with ECharts

I am looking to create a forecast chart that includes both Actual values (presented as a line chart) and projected values (displayed as a dotted chart). An example of what I have in mind can be seen here, created using Excel: Although I couldn't loca ...

Expanding list - Using individual toggles to expand and collapse multiple list items

I stumbled upon an interesting jquery script that allows for a collapsible sub menu within a list. After implementing this code: $('.menu-item-has-children').prepend('<a id="fa-menu" href="#"><i class="fa fa-plus"></i>< ...

retrieve data for chart from an AJAX request

I am looking to create a chart using amCharts and I have received some values from the server through an ajax call. Now, I need help in utilizing this data for my chart. Can anyone guide me on how to achieve this? var chart = am4core.create("chartdiv& ...

Basic Tallying Code in JavaScript

Hi, I am currently working on creating a basic counter using HTML / CSS and Javascript. Unfortunately, my Javascript code is not functioning correctly, even after trying various solutions found online. I attempted to include "async" but it appears that my ...

Making jQuery work: Utilizing tag replacements

My current code is: this.html(this.html().replace(/<\/?([i-z]+)[^>]*>/gi, function(match, tag) { return (tag === 'p') ? match : '<p>'; return (tag === '/p') ? match : '</p& ...

What is the procedure for selecting an element based on its child containing specifically filtered text?

Imagine a webpage with the following elements: <div data-search="type1"> any HTML <!-- .click is a child of any level --> <span class="click" data-source="page1.html">blue</span> <!-- let's call it "click1 ...

The error message "Cannot read property '$scope' of undefined" indicates that there is an issue

After receiving HTML content in an Angular app, I inject it into the HTML document but struggle to retrieve it. For example, you can see a minimized code on plunker here and in JavaScript here Below is my controller: class ReadCtrl constructor: (@$sco ...

JavaScript - exploring techniques to alter the relationship between parents and children

I'm facing an issue with transforming the parent-child relationship in my data structure. Currently, it looks like this: { "id": 7, "name": "Folder 1", "parent_folder": null, "folders": ...

Establish boundaries for D3.js circle reports

I am currently working on a visualization project where I want to arrange cells with higher values to appear towards the top and left, similar to a gravity force. However, I am facing difficulties in keeping multiple circles within the boundaries of the re ...

Unable to send multiple cookies using custom headers in Next.js configuration

I am using custom headers to set the cookie in my next.config.js file. The refresh token is successfully set, but for some reason the second token is not being recognized. key: 'Set-Cookie', value: `RefreshTokenKey = " ...

The system encountered an error while attempting to access the property "getChild" of an unspecified object

I am currently developing a react application and encountering an issue when trying to call a function from the render method. The function I'm trying to call utilizes the getChild method, but I keep receiving an error stating "cannot read property &a ...

Wait for response after clicking the button in Vue before proceeding

When the button is clicked for the first time and the data property is empty, I need to pause the button click code until an ajax response is received. Currently, when I press the button, both wait and scroll actions happen immediately without waiting for ...

What is the best way to create a variable in a React component that requires asynchronously loaded data in order to be functional?

While I have a good understanding of passing data from one component to another using this.props, I am encountering difficulty in asynchronously fetching and storing values, such as from a database, that need to be accessed throughout the component. The ch ...

Tips for preserving an HTML dynamic table in a database

I have a challenge where I am generating a dynamic HTML table using javascript. What is the best way to store this dynamic HTML table in a database using CodeIgniter? This is the code snippet for my input form: <table id="tb3" name="tb3"> & ...

A guide on efficiently traversing a disk directory, recursively fetching all paths in relative format, and converting them into a JSON stream with node.js

My goal is to traverse a directory tree and store it in a JSON file using node.js with the following approach: Open specified directory Convert paths into a relative format. For example: Directory: C:/test { {"C:/test folder 1": [ {"folder 1": ["fil ...

Importing specific files from within the `node_modules` directory can be achieved by

Hello! I am currently using webpack-simple for VueJS and have installed a theme called AdminLTE. I encountered an issue while trying to import the bootstrap files from AdminLTE into my project. When I run `npm run build`, the application searches inside th ...

What is the best way to initiate JavaScript using a button click in WordPress?

I need to add a button to a WordPress page that triggers a JavaScript function when clicked. Here is the HTML code: <!DOCTYPE html> <html lang="en"> <head> <title></title> </head> <body> < ...

Retrieve data from the database by selecting an option from the dropdown menu

I am currently facing a minor issue with the filtering system I am developing. The problem arises when selecting the "All" category from a dropdown menu, which is not part of the database but a separate HTML option. Here is a visual representation of the ...

Having trouble with transferring information from JQuery to PHP

Currently, I'm working on transmitting data from jQuery to PHP. Here's an excerpt of what I've done: var jsonArray = JSON.stringify(dataArray); $.ajax({ type: "POST", url: "addcar_details.php", ...

[filepond] in order to enroll using the serverId value received from the server

Using 'filepond' within a Vue application is causing an issue. In the "process" function, the ID value obtained after transferring the file to the server (response.id) needs to be registered as 'serverId' of the file. Upon checking the ...