在mvc中写api时,没有收到参数如何解决?
通过jquery.post测试成功。后来又通过f12发送,发现始终无法收到参数。
注:我的接口接收参数是一个类对象,没有写[frombody])
[httppost] public myresultinfo index(myparaminfo para) { /// }
解决方法:
在消息头中设置 content-type就可以了。
1、如果你的参数是拼接在url后面,需要设置 content-type: application/x-www-form-urlencoded
2、如果你的参数是通过ajax的data,以json格式提交,需要设置 content-type: application/json
以下是火狐f12发送界面:(注意,自定义时记得修正或删除 content-length,否则会失败,还让你难以发现原因)
参考:https://www.cnblogs.com/tiestoray/p/5032202.html
黄山市民网:https://www.huangshanshimin.com/