首先我们看jquery的ajax设置content-type,我们设置content-type为application/json;代码如下:
$.ajax({那么原生js如何设置content-type呢
type: "POST",
url: "test.php",
data: JSON.stringify({
username: "111", "password": "123"
}),
dataType: "json",
contentType: "application/json;charset=utf-8",
success: function(data) {
if (!data.err) {
console.log(data.msg);
} else {
console.log("出现错误:" + data.msg);
}
},
error: function(jqXHR) {
alert("发生错误:" + jqXHR.status);
},
});
var request = new XMLHttpRequest();那么vue 的axios设置content-type
request.open("POST", "test.php");
request.setRequestHeader("Content-type", "application/json");
request.send(JSON.stringify({
username: "111", "password": "123"
}));
request.onreadystatechange = function() {
if (request.readyState === 4) {
if (request.status === 200) {
var data = JSON.parse(request.responseText);
if (!data.err) {
console.log(data.msg);
} else {
console.log("出现错误:" + data.msg);
}
} else {
alert("发生错误:" + request.status);
}
}
}
axios({那么如果vue-resource
method: 'post',
url: 'test.php',
headers: {
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest'
},
data: {
username: '111',
password: '123'
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
this.$http.get('api', {
headers: {
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest'
},
params: JSON.stringify({
username: "111", "password": "123"
})}).then(function(res) {
document.write(res.body);
}, function(res) {
console.log(res.status);
});
网友回复
DLNA与UPnP的区别和不同?
苏超自建抢票app,通过先预约再抽签化解高并发抢票?
python如何让给电脑在局域网中伪装成电视接收手机的投屏图片视频播放?
如何结合python+js如何自己的视频编码与加密播放直播?
python如何在电脑上通过局域网将本地视频或m3u8视频投屏电视播放?
腾讯视频爱奇艺优酷vip电影电视剧视频如何通过python绕过vip收费直接观看?
有没有可免费观看全球电视台直播m3u8地址url的合集?
有没有实现观影自由的免vip影视苹果 CMS V10 API的可用url?
python如何实时检测电脑usb插入检测报警?
如何判断真人操作的鼠标移动直线轨迹与机器操作的轨迹?