36 lines
1.0 KiB
JavaScript
36 lines
1.0 KiB
JavaScript
function post(url, data = {}, headers = {}) {
|
|
new Promise((resolve, reject) => {
|
|
const xhr = new XMLHttpRequest();
|
|
xhr.open('POST', url);
|
|
|
|
// 设置自定义请求头
|
|
for (const key in headers) {
|
|
if (headers.hasOwnProperty(key)) {
|
|
xhr.setRequestHeader(key, headers[key]);
|
|
}
|
|
}
|
|
|
|
xhr.onload = () => {
|
|
if (xhr.status >= 200 && xhr.status <= 299) {
|
|
resolve(JSON.parse(xhr.responseText));
|
|
} else {
|
|
reject(new Error(`Error: ${xhr.status} - ${xhr.statusText}`));
|
|
}
|
|
};
|
|
xhr.onerror = () => {
|
|
reject(new Error('Network request failed'));
|
|
};
|
|
xhr.send(JSON.stringify(data));
|
|
return responseData;
|
|
});
|
|
}
|
|
|
|
var $builtinmodule = function (name) {
|
|
var mod = {__name__: new Sk.builtin.str("requests")}
|
|
mod.post = new Sk.builtin.func(function(url, json, headers) {
|
|
return post(url,json,headers);
|
|
});
|
|
return mod;
|
|
}
|
|
|