js 原生post请求
js 原生post请求
·
js 原生post请求
let xhr = new XMLHttpRequest(); // 创建XHR对象
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) { // 4表示此次请求结束
console.log("后端返回的结果:" + xhr.responseText);
/** 你的逻辑代码 **/
let result = JSON.parse(xhr.responseText);// 后端返回的结果为字符串,这里将结果转换为json
if (result.code == 1) { // 这里我通过code来标识结果
// 输出后端返回的用户名
console.log("用户名:" + result.data["username"]);
// 输出后端返回的密码
console.log("用户名:" + result.data["password"]);
}
/** 你的逻辑代码End **/
}
};
xhr.open( // 打开链接
"post",
"http://oms4-api-gateway-sit.baozun.com/ecs/ofa/brandFeedback", // 后端地址
true
);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); // 设置请求头
xhr.send( // 设置需要携带到后端的字段,字符串形式
"brandReviewPerson=" + "火柴炮炸牛屎" +
"&brandReviewResults=" + "123456" + // 注意:字段之间需要加上 “ & ” 字符
"&uuid=" + "121" + // 注意:字段之间需要加上 “ & ” 字符
"&brandReviewComments=" + "123456" // 注意:字段之间需要加上 “ & ” 字符
);
更多推荐
所有评论(0)