Axios: getting two requests OPTIONS & POST

35,970

Non-simple CORS requests via AJAX are pre-flighted. Read more about it here. This is a browser behavior and nothing specific to axios. There's nothing inherently wrong with this behavior and if it's working for you, you can just leave it.

If you insist on getting rid of it, there are a few ways you can go about:

  1. You can set Access-Control-Allow-Origin: * on your server to disable CORS.

  2. Make your CORS request a simple one. You will have to change the Content-Type header to application/x-www-form-urlencoded or multipart/form-data or text/plain. No application/json.

I'd say just leave it as it is if the OPTIONS request is not blocking you.

Share:
35,970
Liam
Author by

Liam

Founder & CEO of PaidTabs.com The Complete Package For Music Scores Founder & CEO of iBitcoin.se Advanced cryptocurrency wallet & block explorer. Founder & CEO of Ziggs.io Secure, Easy, Private Messenger, Best to share data & files between developers. Programming knowledge in ReactJS, React Native(IOS & Android), ReduxJS, MobxJS, WebRTC, CSS, Javascript, JSON, Regular Expression. I also developed https://shrroy.github.io/react-snackbar-g/ Link to the package https://github.com/Shrroy/react-snackbar-g What is Ziggs? 💻Ziggs is like a meeting point with your devices, it helps you to transfer pictures, videos, and even APK apps with your friends seamlessly. 🗄Ziggs doesn't store any type of data and doesn't require your personal information. Messages are delivered directly from your device to the connected clients.

Updated on June 09, 2021

Comments

  • Liam
    Liam almost 3 years

    I'm trying to post data. Everything works fine, but I don't know why I'm getting two requests OPTIONS & POST

    POST: enter image description here

    OPTIONS: enter image description here

    Here's the code:

    const url = 'http://rest.learncode.academy/api/johnbob/myusers';
    
    export function postUsers(username, password) {
        let users = {
            username,
            password,
        };
        return{
            type: "USERS_POST",
            payload: axios({
                method:'post',
                url:url,
                data: users,
            })
                .then(function (response) {
                    console.log(response);
                })
                .catch(function (error) {
                    console.log(error);
                })
        }
    }