2019-06-27 post提交数据 编码格式问题 Content-Type accept

accept:客户端告诉服务器我想要什么格式

Content-Type:客户端告诉服务器,我给你传的是什么格式的

常用的为以下三种:

1:application/x-www-form-urlencoded

这是form表单默认的提交格式,提交的参数格式类似于get请求

如果使用ajax提交post请求,需要我们将参数手动拼成get请求格式,再进行urlencode,并且需要设置请求头 

Content-Type=application/x-www-form-urlencoded;charset=utf-8


2:multipart/form-data

这种格式一般用来上传文件,form表单直接设置提交即可。


补充:js模拟表单提交

var formData =new FormData();

formData.append("msg_id", item.msgid);

formData.append("type", 3); //数字123456会被立即转换成字符串 "123456"

formData.append("uin", document.querySelector('#fxx_injector').value);

formData.append("wxdata", data);

var request =new XMLHttpRequest();

request.open("POST", webhook_url);

request.send(formData);

3:application/json

处理复杂json数据结构时,可以考虑使用这种数据传输方式。

使用ajax提交数据时,需要设置请求头

Content-Type=application/json; charset=UTF-8  

同时将需要传递的参数序列化即可(使用JSON.stringify 转成字符串)


4:text/plain

数据以纯文本的形式传递,基本不用。 Postman中标记为raw

参考:

https://imququ.com/post/four-ways-to-post-data-in-http.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。