最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - XHR responseText is empty string - Stack Overflow

programmeradmin2浏览0评论

Html Page:

<!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>

The main.py file:

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

The Request handlers:

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

and,

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

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

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.

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

Html Page:

<!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>

The main.py file:

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

The Request handlers:

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

and,

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

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

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.

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

Share Improve this question asked Oct 23, 2013 at 18:11 tmjtmj 1,8682 gold badges22 silver badges35 bronze badges 1
  • For me the problem was that my backend wasn't using gzip. – Jay Brunet Commented Oct 7, 2017 at 10:58
Add a ment  | 

1 Answer 1

Reset to default 8

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.

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

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

发布评论

评论列表(0)

  1. 暂无评论