我想在我的前端应用程序中使用(本机)promises来执行XHR请求,但没有庞大框架的所有tomfoolery.
我希望我的XHR返回的希望,但是,这并不工作(给我:Uncaught TypeError: Promise resolver undefined is not a function)
function makeXHRRequest (method, url, done) {
var xhr = new XMLHttpRequest();
xhr.open(method, url);
xhr.onload = function() { return new Promise().resolve(); };
xhr.onerror = function() { return new Promise().reject(); };
xhr.send();
}
makeXHRRequest('GET', 'http://example.com')
.then(function (datums) {
console.log(datums);
});
Run Code Online (Sandbox Code Playgroud) 我对xhr返回事件感到困惑,正如我所知,onreadystatechange - > readyState == 4和onload 之间没有太多不同,是真的吗?
var xhr = new XMLHttpRequest();
xhr.open("Get", url, false);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4)
{
/* do some thing*/
}
};
xhr.send(null);
Run Code Online (Sandbox Code Playgroud)
要么
xhr.onload = function() { /* do something */ }
Run Code Online (Sandbox Code Playgroud) 值未替换并且函数返回 0。如何修复它?(react-native 0.30,IOS 10.0模拟器)
export function getCategoryList() {
var xhr = new XMLHttpRequest();
jsonResponse = null;
xhr.onreadystatechange = (e) => {
if (xhr.readyState !== 4) {
return;
}
if (xhr.status === 200) {
console.log('SUCCESS', xhr.responseText);
jsonResponse = JSON.parse(xhr.responseText);
} else {
console.warn('request_error');
}
};
xhr.open('GET', 'https://httpbin.org/user-agent');
xhr.send();
return jsonResponse;
}Run Code Online (Sandbox Code Playgroud)
我正在尝试使用我使用 npm 安装的html2pdf在 javascript 中生成 pdf 。问题是它不会显示来自 pdf 中的 url 的图像。这是我的代码:

这是一个显示问题的沙箱: https://codesandbox.io/s/html2pdf-not-loading-image-gvmx0u
正如您所看到的,当您单击“导出为 pdf”时,内容变量中硬编码的图像没有进入文件。
结果是一个带有空格而不是图像的 pdf 文件。有谁知道如何将我的图片转为pdf文件?js 中有没有更好的库可以从 html 构建 pdf ?