Axios:获取两个请求 OPTIONS &邮政 [英] Axios: getting two requests OPTIONS & POST
问题描述
我正在尝试发布数据.一切正常,但我不知道为什么我收到两个请求 OPTIONS
&POST
I'm trying to post data. Everything works fine, but I don't know why I'm getting two requests OPTIONS
& POST
发布:
选项:
代码如下:
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 请求是预检的.在此处阅读更多相关信息.这是浏览器行为,与 axios 无关.这种行为本身没有任何问题,如果它对你有用,你可以放弃它.
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:
您可以在服务器上设置
Access-Control-Allow-Origin: *
以禁用 CORS.
使您的 CORS 请求变得简单.您必须将 Content-Type
标头更改为 application/x-www-form-urlencoded
或 multipart/form-data
或 文本/纯文本
.没有application/json
.
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
.
如果 OPTIONS
请求没有阻止您,我会说就保持原样.
I'd say just leave it as it is if the OPTIONS
request is not blocking you.
这篇关于Axios:获取两个请求 OPTIONS &邮政的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!