浏览器console发送get或post请求
get请求
var url = "http://******:8080/base/testapi/testcurl?url=https%3A%2F%2Fwww.baidu.com%2F";
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Authorization", "Bearer 1cd2749a-6ecb-4d73-aff1-c35f782a1b71");
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);
};
需要注意的是,get后面的参数在存在特殊字符时,需要进行urlencode。此处可用在线工具:http://www.jsons.cn/urlencode/
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);
};