【问题标题】:Axios: getting two requests OPTIONS & POSTAxios:获取两个请求 OPTIONS & POST
【发布时间】:2018-06-23 15:28:07
【问题描述】:

我正在尝试发布数据。一切正常,但我不知道为什么会收到两个请求 OPTIONSPOST

发布:

选项:

代码如下:

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

【问题讨论】:

标签: javascript reactjs redux react-redux axios


【解决方案1】:

通过 AJAX 的非简单 CORS 请求已预先发送。阅读更多关于它的信息here。这是一种浏览器行为,与 axios 无关。这种行为本质上没有任何问题,如果它对你有用,你可以离开它。

如果你坚持要摆脱它,你可以采取以下几种方法:

  1. 您可以在服务器上设置Access-Control-Allow-Origin: * 以禁用CORS。

  2. 让您的 CORS 请求变得简单。您必须将Content-Type 标头更改为application/x-www-form-urlencodedmultipart/form-datatext/plain。没有application/json

如果OPTIONS 请求没有阻止您,我会说保持原样。

【讨论】:

  • 好收获。修正了措辞。
  • 你好,永顺玩具炉排答案
  • 我明白你的意思,但肯定设置 Access-Control-Allow-Origin: * 不会“禁用 CORS” - 它允许来自任何地方的 CORS 请求。
猜你喜欢
  • 2017-01-11
  • 2020-12-28
  • 2022-11-23
  • 2020-12-15
  • 1970-01-01
  • 2019-12-19
  • 1970-01-01
  • 2017-11-28
  • 2019-09-29
相关资源
最近更新 更多