<tfoot id='Ji8Nc'></tfoot>
      <i id='Ji8Nc'><tr id='Ji8Nc'><dt id='Ji8Nc'><q id='Ji8Nc'><span id='Ji8Nc'><b id='Ji8Nc'><form id='Ji8Nc'><ins id='Ji8Nc'></ins><ul id='Ji8Nc'></ul><sub id='Ji8Nc'></sub></form><legend id='Ji8Nc'></legend><bdo id='Ji8Nc'><pre id='Ji8Nc'><center id='Ji8Nc'></center></pre></bdo></b><th id='Ji8Nc'></th></span></q></dt></tr></i><div id='Ji8Nc'><tfoot id='Ji8Nc'></tfoot><dl id='Ji8Nc'><fieldset id='Ji8Nc'></fieldset></dl></div>
        <legend id='Ji8Nc'><style id='Ji8Nc'><dir id='Ji8Nc'><q id='Ji8Nc'></q></dir></style></legend>

          <bdo id='Ji8Nc'></bdo><ul id='Ji8Nc'></ul>

      1. <small id='Ji8Nc'></small><noframes id='Ji8Nc'>

      2. 将 XMLhttpRequest 变成函数失败:异步还是其他?

        时间:2023-10-13
        <tfoot id='XyXYp'></tfoot>

              • <bdo id='XyXYp'></bdo><ul id='XyXYp'></ul>
                <legend id='XyXYp'><style id='XyXYp'><dir id='XyXYp'><q id='XyXYp'></q></dir></style></legend>

                <i id='XyXYp'><tr id='XyXYp'><dt id='XyXYp'><q id='XyXYp'><span id='XyXYp'><b id='XyXYp'><form id='XyXYp'><ins id='XyXYp'></ins><ul id='XyXYp'></ul><sub id='XyXYp'></sub></form><legend id='XyXYp'></legend><bdo id='XyXYp'><pre id='XyXYp'><center id='XyXYp'></center></pre></bdo></b><th id='XyXYp'></th></span></q></dt></tr></i><div id='XyXYp'><tfoot id='XyXYp'></tfoot><dl id='XyXYp'><fieldset id='XyXYp'></fieldset></dl></div>
                    <tbody id='XyXYp'></tbody>

                  <small id='XyXYp'></small><noframes id='XyXYp'>

                  本文介绍了将 XMLhttpRequest 变成函数失败:异步还是其他?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我尝试将 XMLHttpRequest 变成这样的函数

                  I try to turn an XMLHttpRequest into a function such

                  var getImageBase64 = function (url) { // code function
                      var xhr = new XMLHttpRequest(url); 
                      ... // code to load file 
                      ... // code to convert data to base64
                      return wanted_result; // return result of conversion
                  }
                  var newData = getImageBase64('http://fiddle.jshell.net/img/logo.png'); // function call
                  doSomethingWithData($("#hook"), newData); // reinjecting newData in wanted place.
                  

                  我已成功加载文件并转换为 base64.然而,我一直未能将结果作为输出:

                  I'am successful to load the file, and to convert to base64. I'am however consistenly failling to get the result as an output :

                  var getImageBase64 = function (url) {
                      // 1. Loading file from url:
                      var xhr = new XMLHttpRequest(url);
                      xhr.open('GET', url, true); // url is the url of a PNG image.
                      xhr.responseType = 'arraybuffer';
                      xhr.onload = function(e) { 
                          if (this.status == 200) { // 2. When loaded, do:
                              console.log("1:Response?> " + this.response); // print-check xhr response 
                              var imgBase64 = converterEngine(this.response); // converter
                          }
                      }
                      xhr.send();
                      return xhr.onload(); // <fails> to get imgBase64 value as the function's result.
                  }
                  
                  console.log("4>>> " + getImageBase64('http://fiddle.jshell.net/img/logo.png') ) // THIS SHOULD PRINT THE BASE64 CODE (returned resukt of the function  getImageBase64)
                  

                  请参阅小提琴.

                  See Fiddle here.

                  如何使其工作,以便将新数据作为输出返回?

                  解决方案:我的最终实现是在此处可见,然后继续JS:如何加载位图图像并获取其base64代码?.

                  Solution: my final implementation is visible here, and on JS: how to load a bitmap image and get its base64 code?.

                  推荐答案

                  JavaScript 中的异步调用(如 xhr)无法像常规函数那样返回值.编写异步函数时常用的模式是这样的:

                  Asynchronous calls in JavaScript (like xhr) can't return values like regular functions. The common pattern used when writing asynchronous functions is this:

                  function asyncFunc(param1, param2, callback) {
                    var result = doSomething();
                    callback(result);
                  }
                  
                  asyncFunc('foo', 'bar', function(result) {
                    // result is what you want
                  });
                  

                  所以您的翻译示例如下所示:

                  So your example translated looks like this:

                  var getImageBase64 = function (url, callback) {
                      var xhr = new XMLHttpRequest(url); 
                      ... // code to load file 
                      ... // code to convert data to base64
                      callback(wanted_result);
                  }
                  getImageBase64('http://fiddle.jshell.net/img/logo.png', function(newData) {
                    doSomethingWithData($("#hook"), newData);
                  });
                  

                  这篇关于将 XMLhttpRequest 变成函数失败:异步还是其他?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:依次调用 API 直到响应为空 下一篇:node.js TypeError: path must be absolute or specified root to

                  相关文章

                  最新文章

                • <i id='Ga8IK'><tr id='Ga8IK'><dt id='Ga8IK'><q id='Ga8IK'><span id='Ga8IK'><b id='Ga8IK'><form id='Ga8IK'><ins id='Ga8IK'></ins><ul id='Ga8IK'></ul><sub id='Ga8IK'></sub></form><legend id='Ga8IK'></legend><bdo id='Ga8IK'><pre id='Ga8IK'><center id='Ga8IK'></center></pre></bdo></b><th id='Ga8IK'></th></span></q></dt></tr></i><div id='Ga8IK'><tfoot id='Ga8IK'></tfoot><dl id='Ga8IK'><fieldset id='Ga8IK'></fieldset></dl></div>

                  <small id='Ga8IK'></small><noframes id='Ga8IK'>

                  <legend id='Ga8IK'><style id='Ga8IK'><dir id='Ga8IK'><q id='Ga8IK'></q></dir></style></legend>
                    <bdo id='Ga8IK'></bdo><ul id='Ga8IK'></ul>
                  1. <tfoot id='Ga8IK'></tfoot>