Get
// 创建一个新的 XMLHttpRequest 对象
var xhr = new XMLHttpRequest();
// 设置请求的方法、URL 以及是否异步处理
xhr.open('GET', 'https://api.example.com/data', true);
xhr.setRequestHeader("Content-Type", "application/json");
// 设置请求完成时的回调函数
xhr.onload = function () {
// 检查请求是否成功
if (xhr.status >= 200 && xhr.status < 300) {
// 请求成功,处理数据
var data = JSON.parse(xhr.responseText); // 假设服务器返回的是 JSON 数据
console.log(data); // 在控制台中打印数据
// 在这里你可以进一步处理数据
} else {
// 请求失败,处理错误情况
console.error('Request failed. Returned status of ' + xhr.status);
}
};
// 设置请求失败的回调函数
xhr.onerror = function () {
// 请求过程中出现错误
console.error('Network Error');
};
// 发送请求
xhr.send();
Post
var url = "xxxxx.com/admin/_handle_action_";
var params = {"_key":"6","_model":"App_Model_Event_Event","_action":"Encore_Admin_Grid_Actions_Delete","_input":"true"};
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);
};