Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP常见字段之 Content-Type #11

Open
ZYuMing opened this issue Nov 12, 2018 · 0 comments
Open

HTTP常见字段之 Content-Type #11

ZYuMing opened this issue Nov 12, 2018 · 0 comments
Labels

Comments

@ZYuMing
Copy link
Owner

ZYuMing commented Nov 12, 2018

HTTP常见字段之 Content-Type

字段含义

指http发送信息至服务器时的内容编码类型。就是该请求的里面的信息是图片还是html文件啥的,用于告诉服务器和浏览器怎么解析该请求重的数据。本文主要介绍该字段与在ajax请求中常用的三个值:
application/x-www-form-urlencoded, multipart/form-data, application/json

application/x-www-form-urlencoded

Content-Type:application/json
name=ming&sex=male

FormData对象没有像JSON.stringify那样的方法能批量将对象形式转换为对应的形式

let userObj = {userName: ’xxx', age: '21'}
 formData.append('user', userObj)
 
------WebKitFormBoundaryyb1zYhTI38xpQxBK
Content-Disposition: form-data; name="user"

[object Object]

如需正常转换需手动实现转换函数或者借助qs库

var obj = {
    a: '2', 
    b: {c: 'test'}, 
    c: [ 
        {id: 1, name: 'xx'}, 
        {id:2 ,name: 'yy', info: {d: 4} }
    ]
}



a: 2
b[c]: test
c[][id]: 1
c[][name]: xx
c[][id]: 2
c[][name]: yy
c[][info][d]:4

multipart/form-data

请求体被分割成多部分,每部分使用 --boundary分割;

...
Content-Type: multipart/form-data; boundary=${boundary} 

--${boundary}
...
...

--${boundary}--

json

Content-Type: application/x-www-form-urlencoded
{"name": "ming", "sex":"male"}

对于一些复制的数据对象,对象里面再嵌套数组的话,建议使用application/json。json的形式的优点是它可以传递结构复杂的数据形式,但是json不支持二进制数据,必须转义二进制数据。

@ZYuMing ZYuMing changed the title http提交数据方式(Content-Type字段) HTTP常见字段之 Content-Type Nov 12, 2018
@ZYuMing ZYuMing added the HTTP label Jan 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant