vue,jq,uniapp ajax默认的请求格式
文章目录
- vue,jq,uniapp ajax默认的请求格式
- 常见的内容类型(content-type)
- jq
- 默认请求格式
- 默认内容类型:
- multipart/form-data请求格式
- VUE
-
- uniapp
常见的内容类型(content-type)
- application/x-www-form-urlencoded 表单中默认的encType,表单数据被编码为key/value格式发送到服务器
- application/json json格式
- multipart/form-data 需要在表单中进行文件上传时,就需要使用该格式
jq
默认请求格式
$.ajax({
type: "POST",
url: "login",
//#login为表单id
data: $("#login").serialize(),
dataType: "json",
success: function (response) {
if(response){
window.location.href="success.html"
}else{
alert("fail")
}
},
error: function (response) {
}
});
默认内容类型:
application/x-www-form-urlencoded; charset=UTF-8
multipart/form-data请求格式
$.ajax({
type: "POST",
url: "login",
processData: false,
contentType: false,
data: new FormData($("#login")[0]),
dataType: "json",
success: function (response) {
if(response){
window.location.href="success.html"
}else{
alert("fail")
}
},
error: function (response) {
}
});
VUE
默认请求格式:
this.axios({
method: "请求的方法",
url: "路径",
data: {
数据
},
}).then((response) => {
console.log(response)
})
.catch((response) => {
console.log(response);
});
默认的内容格式
application/json;charset=utf-8
设置请求格式:
this.axios({
method: "请求的方法",
url: "路径",
data: {
数据
},
headers: {
// '键名':'键值'
'Content-Type': 'application/x-www-form-urlencoded'
},
}).then((response) => {
console.log(response)
})
.catch((response) => {
console.log(response);
});
uniapp
uniapp post默认的内容格式我也不太清楚是啥,但是如果不加请求头的话会请求失败,解决方法如下:
uni.request({
url:”路径",
method:"POST",
header:{
"content-type": "application/x-www-form-urlencoded"
},
data:{
//data
},
success(res) {
},
fail(err){
}
})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)