ajax
2020-05-11 本文已影响0人
暖lily
content-type 传输给后台数据类型(后台要的数据格式)
xhr.setRequestHeader ("content-type", "application/x-www-form-urlencoded" )
content-type类型
(1)application/json
json格式 最友好的
(2)multipart/form-data 【默认值】
post表单提交文件时候 form的entype类型设置为 multipart/form-data
例:<form action="/" method="post" enctype="multipart/form-data">
(3)application/x-www-form-urlencoded
浏览器原生form格式,例 key1=val1&key2=val2
(4)text/xml
http做为传输协议,xml为编码风格
dataType 返回数据的格式 常用json
例子
$.ajax({
type: "get",
url: "fa.php",
dataType: "json", //预期服务器返回的数据类型 xml html script json jsonp text
contentType:"text/json",
success: function(data) {
console.log(data);
},
beforeSend: function(xhr) {
//设置请求头 位置+方法
xhr.setRequestHeader("User-Agent", "test");
},
error: function(xhr,error,obj){}
})