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

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

        <legend id='XNfLa'><style id='XNfLa'><dir id='XNfLa'><q id='XNfLa'></q></dir></style></legend>

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

      2. XHR responseText 是空字符串

        时间:2023-10-14

          <tbody id='PFmMx'></tbody>

        <legend id='PFmMx'><style id='PFmMx'><dir id='PFmMx'><q id='PFmMx'></q></dir></style></legend>

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

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

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

                1. 本文介绍了XHR responseText 是空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  HTML页面:

                  <!doctype html>
                  <html lang="en">
                  <head>
                      <meta charset="UTF-8">
                      <title>xhr</title>
                  </head>
                  <body>
                      <script>
                          var xhr_test = new XMLHttpRequest();
                          xhr_test.open("GET","xhrtest",true);
                          xhr_test.send();
                          alert(xhr_test.responseText);
                      </script>
                  </body>
                  </html>
                  

                  main.py 文件:

                  The main.py file:

                  import webapp2
                  from handlers import cookies,pages
                  application = webapp2.WSGIApplication([
                          ('/xhr',pages.XHR),
                          ('/xhrtest', cookies.XHRTest)
                          ],
                              debug=True)
                  

                  请求处理程序:

                  class XHRTest(webapp2.RequestHandler):
                      def get(self):
                          self.response.write('0')
                  

                  和,

                  class XHR(webapp2.RequestHandler):
                      def get(self):
                          f = open('static/html/xhr.html','r')
                          self.response.write(f.read())
                  

                  现在,当我点击 URL localhost:8080/xhrtest 时,浏览器会立即将响应 0 显示为页面内容.

                  Now, when I hit upon the url localhost:8080/xhrtest the browser promptly shows the response 0 as the page's content.

                  点击url localhost:8080/xhr间接点击/xhrtest,在alert框中弹出一个空字符串(responseText是一个空字符串)但是检查在network标签下chrome的response标签,可以看到请求的响应是0.

                  Hitting the url localhost:8080/xhr which indirectly hits /xhrtest, pops up an empty string in the alert box (the responseText is an empty string) but checking chrome's response tab under the network tab, I can see that the request's response is 0.

                  那么为什么 xhr_test.responseText 不能显示相同的响应?

                  So why is xhr_test.responseText not able to display the same response?

                  推荐答案

                  send 调用是异步的(您已将 'async' 参数设置为 true),这意味着您的警报会在请求完成之前立即发生.您应该向 xhr.onreadystatechange 添加一个事件侦听器并在其中使用响应.

                  The call to send is asynchronous (you've set the 'async' parameter to true), which means that your alert is happening immediately, before the request finishes. You should add an event-listener to xhr.onreadystatechange and use the response within that.

                  将true"更改为false"可以使这个简单的示例工作,但在一般情况下不是一个好主意.

                  Changing the 'true' to 'false' would make this simple example work, but is not a good idea in the general case.

                  Ajax 上的 MDN 页面解释了如何使用 XMLHttpRequest.

                  The MDN page on Ajax explains how XMLHttpRequest should be used.

                  这篇关于XHR responseText 是空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:为什么这段代码不起作用?我正在创建一个 Firef 下一篇:AJAX 请求和普通浏览器请求的区别

                  相关文章

                  最新文章

                2. <legend id='fqO1X'><style id='fqO1X'><dir id='fqO1X'><q id='fqO1X'></q></dir></style></legend>
                3. <small id='fqO1X'></small><noframes id='fqO1X'>

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