- 主题:uniapp前端连java后端代码,java什么时候要content-type?何时
uni.request({
url: _this.facilityUrl + 'useridtocid',
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
data: motInfo,
// data: motInfo,
success: (res) => {
}
上面的代码可以正常连接后端java代码,并返回true。但是一开始我少了 header,死活返回false。前端加了header,就返回了true了。用postman测试,headers里也没有要设置content-type。我别的前端代码,有的有header,有的没有。问题是后端如何写的,才要header?怎么样写,才不要header?
--
FROM 120.242.252.*
有时候,我这样写,也行。到底有什么区别?有时候要header,有时候又不要
uni.request({
url: _this.facilityUrl + 'checkResult/createInfo',
method: 'POST',
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify(checkResultInfo),
success: (res) => {
--
FROM 120.242.252.*
uni.request({
url: _this.facilityUrl + 'checkResult/createInfo',
method: 'POST',
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify(checkResultInfo),
success: (res) => {
为何这段代码里,没有出现header,也能用?
【 在 guestking 的大作中提到: 】
: 你可以稍微看一下http协议
: 简单来说ContentType表示你的request的body里面的数据是什么格式的
: 如果你的请求是get,那就没有body,自然就不需要指定ContentType
: ...................
--
FROM 120.242.252.*
问题是,我一开始用contentType,没用,后台返回false。后来改成 header:{content-type:...},就返回true了。postman和swagger都返回true
【 在 guestking 的大作中提到: 】
: 你已经指定的contentType了
: uni.request在创建http request的时候,会自动把这部分内容放到header里
:
--
FROM 120.242.252.*