一:原生ajax请求头
使用setRequestHeader函数:
先调用open方法打开一个url:
xhr.open("post", "/save");
设置数据格式:
1.发送json格式数据:
xhr.setRequestHeader("Content-type","application/json; charset=utf-8");
2.发送表单数据:
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
3.发送纯文本(默认值):
xhr.setRequestHeader("Content-type", "text/plain; charset=utf-8");
4.发送html文本:
xhr.setRequestHeader("Content-type", "text/html; charset=utf-8");
5.编码可带可不带:
// 不带字符编码写法
xhr.setRequestHeader("Content-type", "application/json");
6.值对大小写不敏感:
xhr.setRequestHeader("Content-type","Application/JSON; charset=utf-8");
二:Jquery中ajax设置请求头
全局的:
// 这个是全局的,所有的ajax请求都会加上这个请求头
$(document).ajaxSend(function (event, xhr) {
xhr.setRequestHeader("custom-header", "custom-info") ; // 增加一个自定义请求头
});
局部的:
- 第一种
$('xxx').ajax({
//...
beforeSend:function(jqXHR,options){
jqXHR.setRequestHeader("custom-header", "custom-info") ; // 增加一个自定义请求头
}
//...
}) ;
- 第二种
$('xxx').ajax({
//...
headers:{
"Referer": "http://www.365mini" // 有些浏览器不允许修改该请求头
,"User-Agent": "newLine" // 有些浏览器不允许修改该请求头
,"X-Power": "newLine"
,"Accept-Language": "en-US"
}
//...
}) ;
作者:else05
链接:https://www.jianshu/p/ce9686344781
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
更多推荐
Ajax请求头设置Content-type
发布评论