Home > Technology > Ajax学习之一个post方法(例子)

Ajax学习之一个post方法(例子)

不知道大家注意到我的links页面没有.我写加了搜索评论和查Google PR的功能.今天写个例子实现查PR的例子.基于POST方法.

这个例子用GET方法其实也是很好的.因为传输的数据很少.但是对于一个比较大的表单.项目比较多.输入的字符比较多.那就必须用POST方法了.

下面开始写服务器端查pr的代码.这些代码是我在网上找的,我把它改成我需要的样子.可能不是最好的方法,但是我的要求是能实现查询即可.是在迅雷上找的.源码没有注明作者.这里不好点名感谢了.总之谢谢.哈哈

因为主要介绍js部分这里不贴了.代码到这里下载.

查询页面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>查询Google PR</title>
<script type="text/javascript" src="checkpr.js"></script>
</head>
<body>
	<form  onsubmit="YHLCPR.checkPR();return false" id="checkpr_form" method="post" action="http://philna.com/checkpr.php">
		<label>输入要查询的网址(URL):
			<input type="text" name="checkurl" id="checkprurl" tabindex="1" value="" size="55" onmouseover="this.focus();this.select();"/>
		</label>
		<input type="submit" name="submit" id="submit" value="查询" tabindex="2" />
	</form>
</body>
</html>

javascript代码.

(function(){
if(!window.YHLCPR) window['YHLCPR']={};
 
function $(id){
return document.getElementById(id);
}
 
function getXmlHttpObject(){
	var xmlHttp = null;
	try{
		xmlHttp = new XMLHttpRequest();
	}catch(e){
		try{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}
 
function formToRequestString(form_obj){
	var query_string='';
	var and='&';
	for (i=0;i<form_obj.length ;i++ ){
		e=form_obj[i];
		if (e.name!=''){
			if (e.type=='select-one'){
				element_value=e.options[e.selectedIndex].value;
			}else if (e.type=='checkbox' || e.type=='radio'){
				if (e.checked==false){
					break;	
				}element_value=e.value;
			}else{
				element_value=e.value;
			}
			query_string+=and+e.name+'='+element_value;
		}
	}
	return query_string;
}
 
function stateChangeListener(){
	if(xmlHttp.readyState==1){
	document.body.style.cursor = 'wait';
	$('submit').disabled=true;
	}else if(xmlHttp.readyState==4 && xmlHttp.status==200){
	alert(xmlHttp.responseText);
	document.body.style.cursor = 'auto';
	setTimeout(function(){$('submit').disabled=false;},1000);
	}else if(xmlHttp.status!=200 && xmlHttp.readyState==4){
		document.body.style.cursor = 'auto';
		$('submit').disabled=false;
		alert('呃 出错了 (建议您刷新浏览器.再试)  服务器返回的错误信息:'+xmlHttp.statusText);
	}
}
 
function checkPR(){
	xmlHttp=getXmlHttpObject();
	if (xmlHttp == null) {
		alert ("Oop! Browser does not support HTTP Request.")
		return;
	}
	var url='checkpr.php';
	var data=formToRequestString($('checkpr_form'));
	xmlHttp.onreadystatechange=function(){
		stateChangeListener();
	}
	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlHttp.send(data);
}
window['YHLCPR']['checkPR']=checkPR;
})()

要注意的是:
貌似是ajax默认只能取得当前主机的东西.是这样的吗?因为我在测试的时候出现了这个问题.所以你用这个例子的时候要php环境了.查询pr的文件到我的Google code那里下载吧.

  1. xiaorsz Apr 17th, 2009 @ 19:34 | #1

    居然是沙发!!呵呵!
    你看了哪本 JS 的书啊?

  2. 吖Bee Apr 17th, 2009 @ 20:07 | #2

    ~~Wow!坐个板凳先~

    等我慢慢来研究一下~

    看样子“PhilNa2”好像要横空出世了 :idea:

  3. welee Apr 17th, 2009 @ 20:45 | #3

    嗯,这篇有点深度,要时间慢慢消化才行。
    至于你最后一段说的“ajax默认只能取得当前主机的东西”是什么意思?
    我尝试查了我的域名,一切正常!

  4. Leeiio Apr 17th, 2009 @ 22:05 | #4

    又比咱都先進了,我得慢慢跟上。

  5. Showfom Apr 17th, 2009 @ 23:15 | #5

    糟了,看完以后忘记留言了……

  6. yinheli Apr 18th, 2009 @ 18:54 | #6

    @xiaorsz
    mg12的js哈哈.

    @吖Bee
    太忙了.还有过段时间再弄.

    @welee
    就是不能跨域.我本机测试时就不能直接调用博客的数据.

    @Leeiio
    哪里的话(- -!).你最近jQuery越玩越起劲啊

    @Showfom
    (- -!) 对了那个bug我拖延一段时间了

  7. 会律博客 Apr 20th, 2009 @ 22:06 | #7

    MG12出书了?

  8. yinheli Apr 20th, 2009 @ 22:22 | #8

    @会律博客
    我是指他的js应该是教材上的。写得很优雅.你要是有兴趣可以研究一下.

  9. Y.Jiajia May 13th, 2009 @ 16:09 | #9

    坐下来学习之 :mrgreen:

Submitting Comment, Give me a second...

Leave a comment

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">
Trackbacks & Pingbacks ( 0 )
  1. No trackbacks yet.