简体   繁体   English

Axios:获取两个请求 OPTIONS & POST

[英]Axios: getting two requests OPTIONS & POST

I'm trying to post data.我正在尝试发布数据。 Everything works fine, but I don't know why I'm getting two requests OPTIONS & POST一切正常,但我不知道为什么我收到两个请求OPTIONS & POST

POST:发布: 在此处输入图片说明

OPTIONS:选项: 在此处输入图片说明

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);
            })
    }
}

Non-simple CORS requests via AJAX are pre-flighted.通过 AJAX 的非简单 CORS 请求是预检的。 Read more about it here . 在此处阅读更多相关信息。 This is a browser behavior and nothing specific to axios.这是浏览器行为,与 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.您可以在服务器上设置Access-Control-Allow-Origin: *以禁用 CORS。

  2. Make your CORS request a simple one.使您的 CORS 请求变得简单。 You will have to change the Content-Type header to application/x-www-form-urlencoded or multipart/form-data or text/plain .您必须将Content-Type标头更改为application/x-www-form-urlencodedmultipart/form-datatext/plain No application/json .没有application/json

I'd say just leave it as it is if the OPTIONS request is not blocking you.如果OPTIONS请求没有阻止您,我会说就保持原样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM