AJAX实例学习手记
时间:2006-02-28
来源:互联网
Email:[email protected]
日期:2006-2-27
参考:Using the XML HTTP Request object ,xmlhttp手册
1、由于不同的浏览器对XMLHTTP的支持的不同,所以在创建新的 XMLHttpRequest 对象的时候,需要对浏览器进行判定。
xmlhttp.js的代码:
代码:
var xmlhttp/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp=false;
}
}
@else
xmlhttp=false
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp=false;
}
}
if (!xmlhttp && window.createRequest) {
try {
xmlhttp = window.createRequest();
} catch (e) {
xmlhttp=false;
}
}
包括GET,POST,HEADER等等,xmlhttp发送request的原理,详见前面我转贴的文章http://www.phpchina.cn/bbs/viewthread.php?tid=2727&extra=page%3D1
test.html代码:
代码:
<style type="text/css"><!--
.style1 {color: #FF0000}
-->
</style>
<body>
<script src="xmlhttp.js" type="text/javascript"></script>
<script type="text/javascript">
function RSchange() {
if (xmlhttp.readyState==4) {
document.getElementById('content').innerHTML=xmlhttp.responseText
}
}
function go() {
if (xmlhttp) {
d=document
xmlhttp.open("GET", "test.txt",true);
xmlhttp.onreadystatechange=RSchange
xmlhttp.send(null)
}
}
function headgo()
{
if(xmlhttp){
xmlhttp.open("HEAD",'test.html',true);
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4)
{
alert(xmlhttp.getallResponseHeaders())
}
}
xmlhttp.send(null)
}
}
function testurl()
{
if(xmlhttp){
xmlhttp.open("HEAD",document.getElementById('url').value,true);
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4)
{
if(xmlhttp.status==200) document.getElementById('content3').innerHTML='Url Exist';
else if(xmlhttp.status==404) document.getElementById('content3').innerHTML='this url does not Exist';
else document.getElementById('content3').innerHTML='the return xmlhttp.status:'+xmlhttp.status;
}
}
xmlhttp.send(null)
}
}
</script>
The content will appear between here
<div id=content> </div> and here.<button onclick="go()">Do it</button>
<hr>
Header test!<br>
<button onclick="headgo()">header test</button>
<hr>
Does a url exist?<br>
<input name="url" type="text" id="url" value="http://www.scut.edu.cn"><div class="style1" id=content3> </div>
<br>
<button onclick="testurl()">test a url</button>
test.php
代码:
<?$a=$_GET['a'];
$b=$_GET['b'];
if(is_numeric($a) && is_numeric($b))
{
$total=$a+$b;
}else{
$a='';
$b='';
$total='';
}
if($total<>'')
{
echo $total;
}else{
?>
<script src="xmlhttp.js" type="text/javascript"></script>
<script>
function calc() {
frm=document.forms[0]
url="test.php?a="+frm.elements['a'].value+"&b="+frm.elements['b'].value
xmlhttp.open("GET",url,true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
document.forms[0].elements['total'].value=xmlhttp.responseText
}
}
xmlhttp.send(null)
return false
}
</script>
<form action="test.php" method="get" onsubmit="return calc()">
<input type=text name=a value="<?=$a?>"> + <input type=text name=b value="<?=$b?>"> = <input type=text name=total value="<?=$total?>">
<input type=submit value="Calculate">
</form>
<?
}
?>
作者: feifengxlq 发布时间: 2006-02-27
不过还是顶下。加精。
作者: seraph 发布时间: 2006-02-27
引用:
原帖由 seraph 于 2006-2-27 23:30 发表个人对Ajax不想碰。。。不知道是好还是坏,呵呵。
不过还是顶下。加精。
附上xmlhttp手册吧
作者: feifengxlq 发布时间: 2006-02-28
主文件xmlhttp.html
代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>xmlhttp</title>
</head>
<script language="javascript">
function initxmlhttp()
{
var xmlhttp
try {
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp=false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp=false;
}
}
if (!xmlhttp && window.createRequest) {
try {
xmlhttp = window.createRequest();
} catch (e) {
xmlhttp=false;
}
}
return xmlhttp;
}
function readcontent()
{
var xmlhttp=initxmlhttp();
var showcontent=document.getElementById("showcontent");
var url="readfile.php";
xmlhttp.open("GET",url,true);
xmlhttp.setRequestHeader("Cache-Control","no-cache");
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
showcontent.innerHTML=xmlhttp.responseText;
}
}
xmlhttp.send(null);
}
function writecontent()
{
var xmlhttp=initxmlhttp();
var content=document.forms[0].content.value;
var showcontent=document.getElementById("showcontent");
var url="writefile.php";
var poststr="content="+content;
xmlhttp.open("POST",url,true);
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlhttp.send(poststr);
xmlhttp.onreadyStatechange=function(){
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
showcontent.innerHTML=xmlhttp.responseText;
}
}
}
</script>
<body>
<hr>
<p>AJAX的测试</p>
<p>
<form>
<textarea name="content" cols="50" rows="10" id="content">
</textarea>
<br>
<input type="button" name="Submit" value="读取文本文件数据" onClick="readcontent()">
<input type="button" name="Submit2" value="将数据写入文本文件" onClick="writecontent()"><br>
</form><div id="showcontent"></div>
</p>
<hr>
</body>
</html>
代码:
<?header ("Cache-Control: no-cache, must-revalidate");
echo file_get_contents("test.txt");
?>
代码:
<?file_put_contents('test.txt',$_POST['content']);
echo $_POST['content'];
?>
代码:
this is a ajax example in test.txt[ 本帖最后由 feifengxlq 于 2006-2-28 12:40 编辑 ]
作者: feifengxlq 发布时间: 2006-02-28
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
还有别的头么?是不是发送 post 请求都是发送这个httpheader?
作者: Phzzy 发布时间: 2006-03-08
我寒
var poststr="content="+q;
我在做别的网页的时候
把前面的"content="去掉了.,结果半天取不到值
我差点疯了..
用你的就对,自己的就不对,后来我一行一行注释才发现这个.-.-"
作者: Phzzy 发布时间: 2006-03-11
方便好用
作者: feifengxlq 发布时间: 2006-03-11
引用:
原帖由 Phzzy 于 2006-3-8 03:36 发表看完了
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
还有别的头么?是不是发送 post 请求都是发送这个httpheader?
这个也可以不设置,但推荐设置
作者: feifengxlq 发布时间: 2006-03-11
作者: 帅的不能再输啦 发布时间: 2006-03-22
那我要post多个值要这么呢???
作者: wangyl 发布时间: 2007-04-20
太感谢了,逛顶
作者: php_boy 发布时间: 2007-04-26
作者: php_boy 发布时间: 2007-05-08
作者: syxrrrr 发布时间: 2007-05-15
嘎嘎!
作者: xiaoqiang527 发布时间: 2007-05-15
顶了
支持。。。。
作者: coolkiss 发布时间: 2007-05-16
作者: luohailio 发布时间: 2007-09-28
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28