首先我们看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);
});
网友回复
python如何调用openai的api实现知识讲解类动画讲解视频的合成?
html如何直接调用openai的api实现海报可视化设计及文本描述生成可编辑海报?
f12前端调试如何找出按钮点击事件触发的那段代码进行调试?
abcjs如何将曲谱播放后导出mid和wav格式音频下载?
python如何将曲子文本生成音乐mp3或wav、mid文件
python中mp3、wav音乐如何转成mid格式?
js在HTML中如何将曲谱生成音乐在线播放并下载本地?
python如何实现在windows上通过键盘来模拟鼠标操作?
python如何给win10电脑增加文件或文件夹右键自定义菜单?
python如何将音乐mp3文件解析获取曲调数据?