• <legend id='B1tYu'><style id='B1tYu'><dir id='B1tYu'><q id='B1tYu'></q></dir></style></legend>

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

        <tfoot id='B1tYu'></tfoot>
          <bdo id='B1tYu'></bdo><ul id='B1tYu'></ul>

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

        我怎样才能让 XHR.onreadystatechange 返回它的结果?

        时间:2023-10-13

        1. <tfoot id='7H7xC'></tfoot>

          1. <small id='7H7xC'></small><noframes id='7H7xC'>

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

              <tbody id='7H7xC'></tbody>

              <legend id='7H7xC'><style id='7H7xC'><dir id='7H7xC'><q id='7H7xC'></q></dir></style></legend>
                • <bdo id='7H7xC'></bdo><ul id='7H7xC'></ul>
                • 本文介绍了我怎样才能让 XHR.onreadystatechange 返回它的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我是 JavaScript 编程新手.我现在正在开发我的 Google Chrome 扩展程序.这是不起作用的代码...:P

                  I'm new to JavaScript programming. I'm now working on my Google Chrome Extension. This is the code that doesn't work... :P

                  我想让 getURLInfo 函数返回它的 JSON 对象,并且想把它放到 resp 中.有人可以修复我的代码以使其正常工作吗?

                  I want getURLInfo function to return its JSON object, and want to put it into resp. Could someone please fix my code to get it work?

                  function getURLInfo(url)
                  {
                      var xhr = new XMLHttpRequest();
                      xhr.open
                          (
                              "GET",
                              "http://RESTfulAPI/info.json?url="
                                  + escape(url),
                              true
                          );
                      xhr.send();
                      xhr.onreadystatechange = function()
                      {
                          if (xhr.readyState == 4)
                          {
                              return JSON.parse(xhr.responseText);
                          }
                      }
                  }
                  var resp = getURLInfo("http://example.com/") // resp always returns undefined...
                  

                  提前致谢.

                  推荐答案

                  你在这里处理一个异步函数调用.结果在到达时处理,而不是在函数完成运行时处理.

                  You are dealing with an asynchronous function call here. Results are handled when they arrive, not when the function finishes running.

                  这就是回调函数的用途.当结果可用时调用它们.

                  That's what callback functions are for. They are invoked when a result is available.

                  function get(url, callback) {
                      var xhr = new XMLHttpRequest();
                      xhr.open("GET", url, true);
                      xhr.onreadystatechange = function () {
                          if (xhr.readyState == 4) {
                              // defensive check
                              if (typeof callback === "function") {
                                  // apply() sets the meaning of "this" in the callback
                                  callback.apply(xhr);
                              }
                          }
                      };
                      xhr.send();
                  }
                  // ----------------------------------------------------------------------------
                  
                  
                  var param = "http://example.com/";                  /* do NOT use escape() */
                  var finalUrl = "http://RESTfulAPI/info.json?url=" + encodeURIComponent(param);
                  
                  // get() completes immediately...
                  get(finalUrl,
                      // ...however, this callback is invoked AFTER the response arrives
                      function () {
                          // "this" is the XHR object here!
                          var resp  = JSON.parse(this.responseText);
                  
                          // now do something with resp
                          alert(resp);
                      }
                  );
                  

                  注意事项:

                  • escape() 已被永远弃用.不要使用它,它不能正常工作.使用 encodeURIComponent().
                  • 可以通过设置 open()async 参数使 send() 调用同步为 false.这会导致您的 UI 在请求运行时冻结,而您不希望这样.
                  • 有许多库旨在使 Ajax 请求变得简单而通用.我建议使用其中之一.
                  • escape() has been deprecated since forever. Don not use it, it does not work correctly. Use encodeURIComponent().
                  • You could make the send() call synchronous, by setting the async parameter of open() to false. This would result in your UI freezing while the request runs, and you don't want that.
                  • There are many libraries that have been designed to make Ajax requests easy and versatile. I suggest using one of them.

                  这篇关于我怎样才能让 XHR.onreadystatechange 返回它的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:XMLHttpRequest responseType = "json";给出错误 S 下一篇:HTML5 FormData 在 Java Servlet request.getParameter() 中返回

                  相关文章

                  最新文章

                • <small id='B5FnU'></small><noframes id='B5FnU'>

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

                    • <bdo id='B5FnU'></bdo><ul id='B5FnU'></ul>

                      <tfoot id='B5FnU'></tfoot>
                      <legend id='B5FnU'><style id='B5FnU'><dir id='B5FnU'><q id='B5FnU'></q></dir></style></legend>