使用Fetch发送post请求,总是传值失败,最后发现这篇文章,试了下可用,记录一下。
使用 Vue axios插件调用.net Core方法的三种方式
第一种,动态类型接收
var a = { 'a': 1, 'b': 2 };
axios({
url: '/Login/Register',
method: 'post',
data: JSON.stringify(a),
headers: {
'Content-type': 'application/json'
},
}).then(res => {
console.log(1234, res.data)
}).catch(error => {
console.log(error)
})
[HttpPost]
public string Register([FromBody] dynamic jsonData)
{
string data = Convert.ToString(jsonData);
return data;
}
第二种,字典类型接收
var a= { 'jsonData': 1 };
axios({
url: '/Login/Registerdy',
method: 'post',
data: { json: JSON.stringify(a)} ,
headers: {
'Content-type': 'application/json'
}
}).then(res => {
console.log(1234, res.data)
}).catch(error => {
console.log(error)
})
[HttpPost]
public string Registerdy([FromBody] Dictionary<string, string> json)
{
return json["jsonData"];
}
第三种,实体类接收
var a = { 'a': 1, 'b': 2 };
axios({
url: '/Login/Registermo',
method: 'post',
data: { jsonData: JSON.stringify(a)} ,
headers: {
'Content-type': 'application/json'
}
}).then(res => {
console.log(1234, res.data)
}).catch(error => {
console.log(error)
})
[HttpPost]
public string Registermo([FromBody] test model)
{
return model.jsonData;
}
public class test
{
public string a{ get; set; }
public string b{ get; set;}
}
如果侵犯到原作者,请联系我删除。
原文链接:https://blog.csdn.net/tx1721110240/article/details/108668795