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 ファイル:
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
をページのコンテンツとしてすぐに表示します。
localhost:8080/xhr
間接的にヒットする urlをヒット/xhrtest
すると、アラート ボックスに空の文字列がポップアップ表示されます (responseText は空の文字列です) 0
。
では、なぜxhr_test.responseText
同じ応答を表示できないのでしょうか。