setRequestHeader 设置请求头
设置请求头前需先调用open方法打开一个url
xhr.open("post", "/save");
- 1
- 1
设置数据格式
发送json格式数据
xhr.setRequestHeader("Content-type","application/json; charset=utf-8");
- 1
- 1
发送表单数据
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
- 1
- 1
发送纯文本
不指定Content-type时,此是默认值值
xhr.setRequestHeader("Content-type", "text/plain; charset=utf-8");
- 1
- 1
发送html文本
xhr.setRequestHeader("Content-type", "text/html; charset=utf-8");
- 1
- 1
编码可带可不带
// 不带字符编码写法
xhr.setRequestHeader("Content-type", "application/json");
- 1
- 2
- 3
- 1
- 2
- 3
值对大小写不敏感
xhr.setRequestHeader("Content-type","Application/JSON; charset=utf-8");
- 1
- 1
虽然如此,但还是都保持小写