Google App Engine中的基本Ajax实现

时间:2014-08-12 18:11:16

标签: javascript ajax google-app-engine

我一直在Google App Engine上开发Web应用程序已有一段时间了,但这是我第一次尝试在应用程序中实现Ajax。由于我熟悉Javascript概念,所以我从W3C的Ajax教程开始,虽然我已经开始了解Ajax是如何实现的,但是我怀疑需要一些帮助。

我尝试将我学到的东西实现到基本的Web应用程序中:

main.py

class AjaxHandler(Handler):
    def get(self):
        self.render("gg.html")    

app = webapp2.WSGIApplication(('/ajax', AjaxHandler)], debug=True)

gg.html

<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","aboutus.txt",true);
xmlhttp.send();
}
</script>
</head>
<body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

</body>
</html>

'aboutus.txt'文件保存在我的项目目录中。我不知道我的方法是否正确,但我希望'gg.html'页面中的默认文本被我的.txt文件中的文本替换。

现在,我知道在xmlhttp.open("GET","aboutus.txt",true);行中,第二个参数是文件存储在服务器上的URL。有人可以指出我的代码有什么问题吗?还提供了一种正确的方法来更改网页上的默认文本,使用Ajax中保存在项目目录中的.txt文件中提供的文本?

0 个答案:

没有答案
相关问题