gpt4 book ai didi

javascript - Axios:获取两个请求 OPTIONS & POST

转载 作者:可可西里 更新时间:2023-11-01 02:20:17 25 4
gpt4 key购买 nike

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

发布: enter image description here

选项: enter image description here

代码如下:

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

最佳答案

通过 AJAX 的非简单 CORS 请求是预飞行的。了解更多信息 here .这是一种浏览器行为,与 axios 无关。这种行为本身并没有什么错,如果它对你有用,你可以离开它。

如果你坚持要摆脱它,有几种方法可以解决:

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

  2. 让您的 CORS 请求变得简单。您必须将 Content-Type header 更改为 application/x-www-form-urlencodedmultipart/form-data文本/纯文本。不是 application/json

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

关于javascript - Axios:获取两个请求 OPTIONS & POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48255545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com