浏览器发送POST请求、DELETE请求
1.浏览器发送POST请求
方法一:
var xml = new XMLHttpRequest();
var url = "http://127.0.0.1:8800/admin/user";
xml.open("POST", url, true);
xml.setRequestHeader("Content-type", "application/json");
xml.setRequestHeader("Authorization","xxx");
xml.send('{"model": {"name": "测试接口","remark": "<p>测试接口接口<br></p>"}}');
方法二:
var url = "http://localhost:8800/admin/user"; //请求路径
var params = {
"model": {
"name": "测试接口",
"remark": "<p>测试接口接口<br></p>"
}
}; //此为json格式的参数
var xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Authorization","xxx");
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);
};
2. 浏览器发送DELETE请求
var xml = new XMLHttpRequest();
var url = "http://127.0.0.1:8800/admin/user/111000";
xml.open("DELETE", url, true);
xml.setRequestHeader("Content-type", "application/json");
xml.setRequestHeader("Authorization","xxx");
xml.send();