简体   繁体   English

在axios中设置Content-Type标头会导致没有数据发送

[英]Setting Content-Type header in axios causes no data is sent

I'm using axios to POST data to WebApi and I'm trying to set Content-Type to application/json, but when I'm adding headers to config no data is sent. 我正在使用axios将数据发布到WebApi,并且尝试将Content-Type设置为application / json,但是当我向配置中添加标头时,不会发送任何数据。 My axios request: 我的axios请求:

axios({
    method: 'post',
    headers: {'Content-Type': 'application/json',},
    url: `${URL}/Signin`,
    data: user  
});

and it results with request: 结果与请求:

OPTIONS http://localhost:50644/api/Signin HTTP/1.1
Host: localhost:50644
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: http://localhost:3000
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36
Access-Control-Request-Headers: content-type
Accept: */*
Referer: http://localhost:3000/Signin
Accept-Encoding: gzip, deflate, br
Accept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4

When I remove headers from my axios request all is ok, but Content Type is wrong. 当我从axios请求中删除标题时,一切正常,但“内容类型”错误。

POST http://localhost:50644/api/Signin HTTP/1.1
Host: localhost:50644
Connection: keep-alive
Content-Length: 40
Accept: application/json, text/plain, */*
Origin: http://localhost:3000
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: http://localhost:3000/Signin
Accept-Encoding: gzip, deflate, br
Accept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4
{
    "Name": "test",
    "Password": "test"
}

What I'm doing wrong? 我做错了什么? Thank you in advance for your help! 预先感谢您的帮助!

我无法确定是否不知道user的数据结构是什么样子,但我想您需要处理data: JSON.stringify(user)user转换为JSON格式。

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

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