一直以来,都有个错误概念,就是Ajax很复杂,今天要用到Ajax,搜karrigell和Ajax,没有内容,结果发现我的javascript脚本里调用xml的方法就是Ajax
详细的
贴个简单的例子:(testajax.html和ajax.py,把两个文件放到karrigell下的www目录里,启动karrigell)
testajax.html
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
-
<html>
- ajax.py
<HEAD>
-
<TITLE> testajax </TITLE>
-
<script language="JavaScript">
-
var xmlHttp;
-
function createXMLHttpRequest() {
-
if (window.ActiveXObject) {
-
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
-
} else if (window.XMLHttpRequest) {
-
xmlHttp = new XMLHttpRequest();
-
}
-
}
-
function startRequest() {
-
createXMLHttpRequest();
-
xmlHttp.onreadystatechange = handleStateChange;
-
var qurl = "ajax.py";//发送请求到的URL地址
-
xmlHttp.open("GET", qurl, true);
-
xmlHttp.send(null);
-
}
-
function handleStateChange() {
-
if(xmlHttp.readyState == 4) {
-
if(xmlHttp.status == 200) {
-
document.getElementById('txt').innerHTML = xmlHttp.responseText;
-
}
-
}
-
}
-
</script>
-
</HEAD>
-
<body >
- hello world!
-
<input type="button" value="发送" onClick="startRequest()">
-
<span id="txt">source</span>
-
</body>
ajax.py
- #!/usr/bin/env python
-
# -*- coding: UTF-8 -*-
-
print "ajax is ok"
通过karrigell访问testajax.html,点击按钮,source在页面不刷新的情况下被更新为ajax is ok
阅读(1285) | 评论(0) | 转发(0) |