F12打开Console输入以下代码:
POST请求
var url = "http://ip地址:8080/test/?id=26323";
var params = {"billIds":["56141305725718528"],"billType":"VBNSC"};
var xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onload = function (e) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
console.log(xhr.responseText);
} else {
console.error(xhr.statusText);
}
}
};
xhr.send(JSON.stringify(params));
xhr.onerror = function (e) {
console.error(xhr.statusText);
};
GET请求
var url = "http://IP:8080/testGet?id=235";
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
// xhr.setRequestHeader("Content-Type", "application/json");
xhr.onload = function (e) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
console.log(xhr.responseText);
} else {
console.error(xhr.statusText);
}
}
};
xhr.send();
xhr.onerror = function (e) {
console.error(xhr.statusText);
};
F12发起GET与POST请求示例
本文提供了一个使用F12开发者工具通过Console发起GET和POST请求的示例。示例中包含了设置请求头、处理响应及错误的方法。
573

被折叠的 条评论
为什么被折叠?



