本文共 2080 字,大约阅读时间需要 6 分钟。
/** * 原生 JavaScript 的 Ajax 函数 * @type { {get: Ajax.get, post: Ajax.post}} */ const Ajax = { get: function(url, fn) { // 使用 XMLHttpRequest 对象进行数据交互 var xhr = new XMLHttpRequest(); xhr.open('GET', url, true); xhr.onreadystatechange = function() { // 当请求完成时处理回应 if ((xhr.readyState == 4 && xhr.status == 200) || xhr.status == 304) { fn.call(this, xhr.responseText); } }; xhr.send(); }, post: function(url, data, fn) { // 使用 XMLHttpRequest 发送 POST 请求 var xhr = new XMLHttpRequest(); xhr.open('POST', url, true); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.onreadystatechange = function() { // 处理服务器返回的响应 if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 304)) { fn.call(this, xhr.responseText); } }; xhr.send(data); } }; export function uploadProject(_project) { // 将项目对象转换为 JSON 格式 const project = _project.toJS(); const sideJson = JSON.stringify(project); ModalState.showAlert({ title: '上传云端', description: sideJson, confirmLabel: '确定', cancelLabel: '取消' }, choseUpload => { if (choseUpload) { // 服务器地址配置(示例) const host = 'https://localhost:9000'; const token = uuidv4(); const data = { name: project.name, sideJson: sideJson, token: token }; // 发送 POST 请求 Ajax.post(`${host}/uitestcase/upload.api`, JSON.stringify(data), res => { console.log(res); }); } }); }
转载地址:http://nsea.baihongyu.com/