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

Is it possible to simultaneously run multiple functions with event listeners on a canvas?

I'm attempting to create a canvas function that displays the real-time mouse cursor location within the canvas and, upon clicking, should draw a circle. I came across this code snippet that reveals the x and y coordinates of the mouse: document.addEve ...

Creating a stylish gradient text color with Material-UI's <Typography /> component

Is there a way to apply a gradient font color to a <Typography /> component? I've attempted the following: const CustomColor = withStyles({ root: { fontColor: "-webkit-linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)", }, })(T ...

Creating grids and dividing them using a combination of CSS, JavaScript, and PHP

I've encountered an interesting challenge while working on a web project and would love some advice on how to tackle it: Within a database, I have a collection of images that I want to display in a grid format. The twist is that higher ranked images ...

Using AJAX to call a PHP function within a PHP script

I have successfully implemented an AJAX file that can execute content from a PHP file. However, my concern is how to specifically call a particular function within the PHP file if there are multiple functions present. Below is my current AJAX code: $(doc ...

What is the method to alter the color of an SVG source within an image tag?

I am currently working on a component that involves changing the color of an SVG icon from black to white when a color prop is given. export class CategoryIconComponent extends React.Component { static displayName = 'CategoryIcon'; state = ...

Discovering all jQuery functions employed in a JavaScript file can be accomplished through the utilization of regular expressions

I have a somewhat unconventional idea that I want to share. The project I'm currently working on has an old front-end codebase that is causing the website to run slowly, with jQuery being a major factor. My plan is to develop a tool that can analyze a ...

Implement Next.js deployment on NGINX server with a 403 forbidden error

I am currently utilizing Next.js for the frontend and Django for the backend. During development, everything is functioning properly. However, when transitioning to production, I am encountering a 403 Forbidden Error related to /_next/static/chunks. It app ...

"Troubleshooting: IE compatibility issue with jQuery's .each and .children functions

I have created an Instagram image slider feed that works well in Chrome, Safari, Firefox, and Opera. However, it fails to function in all versions of Internet Explorer without showing any error messages. To accurately determine the height of images for th ...

How can jQuery identify the elements that the selected text spans across?

In the midst of a project, I find myself in need of extracting specific text within an iframe. This text may traverse across multiple spans within the iframe, each identified by a unique ID spanning from 1 to the end of the page. My goal is not to retrie ...

What could be causing the malfunction of the Facebook iframe like button?

Even though it functions offline, there seems to be an issue with its performance online. Here is the code that was used: <iframe src="http://www.facebook.com/plugins/like.php?app_id=125925834166578&amp;href=http%3A%2F%2Fwww.facebook.com%2FBaradei. ...

Is it possible to remove the v-main element?

Can anyone help me figure out what the big purple square at the bottom of my page is and how to delete it? https://i.stack.imgur.com/bMVOu.jpg <template> <v-app> <template> <header> <div> <Header ...

Error: The property 'setCrossOrigin' is not defined and cannot be read

When I try to run both .obj and .mtl files together, I encounter an error, whereas running just the .obj loader works fine. The specific error message that appears is: MTLLoader error Below is the code snippet I am using to load the .obj and .mtl files: ...

"Implementation of Google+ button causing the appearance of a horizontal scrollbar

Adding Facebook and Twitter sharing buttons was easy, but now I'm having trouble with Google+. No matter where I place the code on my page (using a Bootstrap grid), it always adds 2-3 pixels on the right side, creating a horizontal scrollbar: <div ...

How can a chat script be created efficiently without needing Server Root Access?

I currently have a hosting account (cPanel or DirectAdmin) where I don't have root access and am unable to use exec() or shell_exec() functions due to restrictions set by the server administrator. While I understand that socket programming is conside ...

Intercepting HTTP requests on specific routes with Angular 4+ using an HTTP Interceptor

I've developed an HTTP_INTERCEPTOR that needs to function on certain routes while excluding others. Initially, it was included in the main app module file. However, after removing it from there and adding it to specific modules, the interceptor conti ...

Can you provide guidance on implementing structured data jsonLD in a Next.js 13 application's router?

I have been struggling to implement structured data in my Next.js 13 (app router) application and have not been able to find the correct method. The next-seo package is also throwing errors for me. When I tried using next-seo, I encountered this error: ...

Experience the ability to activate row-click functionality in Element UI based on specific conditions with VueJS

Could someone please advise me on how to enable the row click event in Element UI only based on a certain condition? Specifically, I want the row to be clickable only if the status is 'Approved' <el-table @row-click="view"> <!- ...

What steps do I need to take to translate jQuery code into standard JavaScript code?

Can someone please help me convert this jQuery code to plain JavaScript? window.addEventListener('scroll', function() { document.querySelectorAll('.container p').forEach(function(el) { var scrollTop = window.scrollY, ...

Solution for Vue index.html Favicon problem

Having some trouble with my favicon.ico/png file in Vue 2.0 and Vue CLI 3.0. No matter what I try, it just won't work properly. I've located the problem to this specific issue. After running yarn build, the index.html file changes this line of ...

Executing PHP include statement with ajax: A step-by-step guide

I am trying to achieve a feature on my website where when a button is clicked in the index.php file, an AJAX request will load about.php and display it inside a div tag within index.php. Unfortunately, my current code doesn't seem to be working as exp ...