Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
509 views
in Technique[技术] by (71.8m points)

javascript - XHR responseText is empty string

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...