美文网首页APP & programuniapp
Uniapp~动态修改 请求头的content-type 的值

Uniapp~动态修改 请求头的content-type 的值

作者: 扶得一人醉如苏沐晨 | 来源:发表于2022-10-21 11:57 被阅读0次

在uniapp官网有这样一段话:

header默认的请求时 header['content-type'] 为 application/json

对于 POST 方法且 header['content-type'] 为 application/json 的数据,会进行 JSON 序列化。

对于 POST 方法且 header['content-type'] 为 application/x-www-form-urlencoded 的数据,会将数据转换为 query string。

小程序或者uniapp使用request发起请求时一般method有GET和POST

不设置method也不设置content-type默认是下面这段
      method: "GET",

      header: {

        'content-type': 'application/json'

      },

设置了method为post

content-type默认是application/x-www-form-urlencoded

用POST请求时,有两种情况,

第一种:如果后端在Query里接收参数,header需要设置content-type': 'application/json 代码如下

header: {

  'content-type': 'application/json'//后端接收的是json对象类型 例如{ 'id':'1231454', 'sex':'男' } },

如果在Body里接收header 需要设置content-type': 'application/x-www-form-urlencoded' 代码如下

header: {

        'content-type': 'application/x-www-form-urlencoded'//后端接收的是(表单)字符串类型,例如'id=1231454&sex=男'},

如何动态修改 请求头的content-type

 在这里传入自定义的content-type可以改变默认

相关文章

网友评论

    本文标题:Uniapp~动态修改 请求头的content-type 的值

    本文链接:https://www.haomeiwen.com/subject/honpzrtx.html