<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Erick &#187; AJAX</title>
	<atom:link href="http://blog.xiongchuan.org/tag/ajax/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.xiongchuan.org</link>
	<description>Even a great life is only a life until you make it.</description>
	<lastBuildDate>Thu, 10 Mar 2011 14:44:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
	
<!-- Start Of Script Generated By WP-PostViews Plus -->
<script type='text/javascript' src='http://blog.xiongchuan.org/wp-includes/js/jquery/jquery.js?ver=1.4.4'></script>
<script type="text/javascript">
/* <![CDATA[ */
/* ]]> */
</script>
<!-- End Of Script Generated By WP-PostViews Plus -->
	<item>
		<title>jQuery,callback&#124;回调函数的问题</title>
		<link>http://blog.xiongchuan.org/jquery-callback-function-something.html</link>
		<comments>http://blog.xiongchuan.org/jquery-callback-function-something.html#comments</comments>
		<pubDate>Wed, 07 Oct 2009 06:44:29 +0000</pubDate>
		<dc:creator>Erick</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[回调函数]]></category>

		<guid isPermaLink="false">http://www.pigblog.net/?p=307</guid>
		<description><![CDATA[用jquery一直都是用的异步传输，习惯了，今天在做表单提交验证一个字段的惟一性的时候，同样是用了异步，问题就出现了。 var checkwap=function(){ var total; $.ajax({ url:'bsmajax.php?type=checkwap', type:'GET', async:false,//原来是true, cache:false, data :{wapnum:$('input[id=wapnum]').val()}, dataType :'json', success :function(json){ total=json.total; } }); return total; }; alert(checkwap());//开始的时候怎么也得不到值，以为是jquery关于变量作用域的访问问题 多加了几个alert();后才觉悟，因为是异步的，alert(checkwap());先执行了，等ajax成功返回数据后total才有值。 因为在表单提交的验证是要实时的，所以就 async:false 了，变成同步的。 当我以为是变量作用域问题时，在GOOGLE里搜索时，也有人犯这样的迷糊，现在记下来。 Related Postsjavascript学习AJAX五步走,POST方法学习手记]]></description>
			<content:encoded><![CDATA[<p>用jquery一直都是用的异步传输，习惯了，今天在做表单提交验证一个字段的惟一性的时候，同样是用了异步，问题就出现了。</p>
<pre name="code" class="javascript">
	var checkwap=function(){
		var total;
		$.ajax({
			url:'bsmajax.php?type=checkwap',
			type:'GET',
			async:false,//原来是true,
			cache:false,
			data :{wapnum:$('input[id=wapnum]').val()},
			dataType :'json',
			success :function(json){
				total=json.total;
			}
		});
		return total;
	};
alert(checkwap());//开始的时候怎么也得不到值，以为是jquery关于变量作用域的访问问题
</pre>
<p>多加了几个alert();后才觉悟，因为是异步的，alert(checkwap());先执行了，等ajax成功返回数据后total才有值。<br />
因为在表单提交的验证是要实时的，所以就 async:false 了，变成同步的。</p>
<p>当我以为是变量作用域问题时，在GOOGLE里搜索时，也有人犯这样的迷糊，现在记下来。</p>
<h4  class="entry-title">Related Posts</h4><ul class="related_post"><li><a href="http://blog.xiongchuan.org/javascript-study.html" title="javascript学习">javascript学习</a></li><li><a href="http://blog.xiongchuan.org/to-study-ajax-follow-me.html" title="AJAX五步走,POST方法学习手记">AJAX五步走,POST方法学习手记</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://blog.xiongchuan.org/jquery-callback-function-something.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AJAX五步走,POST方法学习手记</title>
		<link>http://blog.xiongchuan.org/to-study-ajax-follow-me.html</link>
		<comments>http://blog.xiongchuan.org/to-study-ajax-follow-me.html#comments</comments>
		<pubDate>Mon, 15 Jun 2009 08:30:50 +0000</pubDate>
		<dc:creator>Erick</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[AJAX]]></category>

		<guid isPermaLink="false">http://www.wp.com/wordpress/?p=81</guid>
		<description><![CDATA[本文原创，转载请注明出处，谢谢合作！ AJAX 关于这个技术的书和文章，我不知道看了多少，一直没有用过。 PigBlog主要功能里就只有发表评论了，想想就用AJAX的吧，开始操刀AJAX,下面是我用这个技术的时候的学习心得。 从整体上来说，就是在JS里程序然后，通过DOM操作获得数据，发送给后台的PHP文件，PHP文件处理，返回结果，JS再接收，再通过DOM操作显示数据。 思路就是这样，其中难点是JS文件的编写。这整个过程中，我用到的一些东西，XMLHttpRequest,XML,DOM. 第一步：建立XMLHttpRequest，怎么建立就不说了，网上多的方法，我只把我的代码发出来 JAVASCRIPT //创建XMLHTTP function CreateXMLHttp(){ var request=false; try{ if(window.XMLHttpRequest){ request=new XMLHttpRequest(); /* * 如果服务器的响应没有XML mime-type header，某些Mozilla浏览器可能无法正常工作。 * 为了解决这个问题，如果服务器响应的header不是text/xml，可以调用其它方法修改该header。 * From:http://leexuan.blogbus.com/logs/1935602.html * */ if(request.overrideMimeType){ request.overrideMimeType(&#8216;text/xml&#8217;); } } else if(window.ActiveXObject){ var i=0; var xmlarray=[ 'Microsoft.XMLHTTP', 'MSXML.XMLHTTP', 'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP' ]; var len=xmlarray.length; for(i;i&#60;len;++i){ try{ request=new ActiveXObject(xmlarray[i]); // request.SetRequestHeader(&#8216;Content-Type&#8217;,'text/xml&#8217;); request.SetRequestHeader(&#8216;Content-Type&#8217;,&#8216;UTF-8&#8242;); [...]]]></description>
			<content:encoded><![CDATA[<p><span style="font-size: 14px; color: #ff0000; line-height: 24px;">本文原创，转载请注明出处，谢谢合作！</span><br />
AJAX 关于这个技术的书和文章，我不知道看了多少，一直没有用过。<br />
PigBlog主要功能里就只有发表评论了，想想就用AJAX的吧，开始操刀AJAX,下面是我用这个技术的时候的学习心得。</p>
<p>从整体上来说，就是在JS里程序然后，通过DOM操作获得数据，发送给后台的PHP文件，PHP文件处理，返回结果，JS再接收，再通过DOM操作显示数据。<br />
思路就是这样，其中难点是JS文件的编写。这整个过程中，我用到的一些东西，XMLHttpRequest,XML,DOM.</p>
<p>第一步：建立XMLHttpRequest，怎么建立就不说了，网上多的方法，我只把我的代码发出来</p>
<div style="border: 1px dashed #cccccc; font-weight: normal; font-size: 12px; line-height: 18px; font-style: normal; font-variant: normal;">
<div style="border-bottom: 1px dashed #cccccc; padding: 5px 10px; font-weight: bold; line-height: 20px; height: 20px;">JAVASCRIPT</div>
<ol>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="color: #006600; font-style: italic;">//创建XMLHTTP</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="font-weight: bold; color: #003366;">function</span> CreateXMLHttp<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="font-weight: bold; color: #003366;">var</span> request<span style="color: #339933;">=</span><span style="font-weight: bold; color: #003366;">false</span><span style="color: #339933;">;</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="font-weight: bold; color: #000066;">try</span><span style="color: #009900;">{</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="font-weight: bold; color: #000066;">if</span><span style="color: #009900;">(</span>window.<span style="color: #660066;">XMLHttpRequest</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;">request<span style="color: #339933;">=</span><span style="font-weight: bold; color: #003366;">new</span> XMLHttpRequest<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="color: #006600; font-style: italic;">/*</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="color: #006600; font-style: italic;"> * 如果服务器的响应没有XML mime-type header，某些Mozilla浏览器可能无法正常工作。</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="color: #006600; font-style: italic;"> * 为了解决这个问题，如果服务器响应的header不是text/xml，可以调用其它方法修改该header。</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="color: #006600; font-style: italic;"> * From:http://leexuan.blogbus.com/logs/1935602.html</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="color: #006600; font-style: italic;"> * */</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="font-weight: bold; color: #000066;">if</span><span style="color: #009900;">(</span>request.<span style="color: #660066;">overrideMimeType</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;">request.<span style="color: #660066;">overrideMimeType</span><span style="color: #009900;">(</span><span style="color: #3366cc;">&#8216;text/xml&#8217;</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="color: #009900;">}</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="color: #009900;">}</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="font-weight: bold; color: #000066;">else</span> <span style="font-weight: bold; color: #000066;">if</span><span style="color: #009900;">(</span>window.<span style="color: #660066;">ActiveXObject</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="font-weight: bold; color: #003366;">var</span> i<span style="color: #339933;">=</span><span style="color: #cc0000;">0</span><span style="color: #339933;">;</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="font-weight: bold; color: #003366;">var</span> xmlarray<span style="color: #339933;">=</span><span style="color: #009900;">[</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="color: #3366cc;">'Microsoft.XMLHTTP'</span><span style="color: #339933;">,</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="color: #3366cc;">'MSXML.XMLHTTP'</span><span style="color: #339933;">,</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="color: #3366cc;">'Msxml2.XMLHTTP.6.0'</span><span style="color: #339933;">,</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="color: #3366cc;">'Msxml2.XMLHTTP.5.0'</span><span style="color: #339933;">,</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="color: #3366cc;">'Msxml2.XMLHTTP.4.0'</span><span style="color: #339933;">,</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="color: #3366cc;">'Msxml2.XMLHTTP.3.0'</span><span style="color: #339933;">,</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="color: #3366cc;">'Msxml2.XMLHTTP'</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="color: #009900;">]</span><span style="color: #339933;">;</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="font-weight: bold; color: #003366;">var</span> len<span style="color: #339933;">=</span>xmlarray.<span style="color: #660066;">length</span><span style="color: #339933;">;</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="font-weight: bold; color: #000066;">for</span><span style="color: #009900;">(</span>i<span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>len<span style="color: #339933;">;++</span>i<span style="color: #009900;">)</span><span style="color: #009900;">{</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="font-weight: bold; color: #000066;">try</span><span style="color: #009900;">{</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;">request<span style="color: #339933;">=</span><span style="font-weight: bold; color: #003366;">new</span> ActiveXObject<span style="color: #009900;">(</span>xmlarray<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="color: #006600; font-style: italic;">// request.SetRequestHeader(&#8216;Content-Type&#8217;,'text/xml&#8217;);</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;">request.<span style="color: #660066;">SetRequestHeader</span><span style="color: #009900;">(</span><span style="color: #3366cc;">&#8216;Content-Type&#8217;</span><span style="color: #339933;">,</span><span style="color: #3366cc;">&#8216;UTF-8&#8242;</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="font-weight: bold; color: #000066;">break</span><span style="color: #339933;">;</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="color: #009900;">}</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="font-weight: bold; color: #000066;">catch</span><span style="color: #009900;">(</span>e<span style="color: #009900;">)</span><span style="color: #009900;">{</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;">request<span style="color: #339933;">=</span><span style="font-weight: bold; color: #003366;">false</span><span style="color: #339933;">;</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="color: #009900;">}</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="color: #009900;">}</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="color: #009900;">}</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="font-weight: bold; color: #000066;">else</span><span style="color: #009900;">{</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="color: #000066;">alert</span><span style="color: #009900;">(</span><span style="color: #3366cc;">&#8216;你的浏览器不支持AJAX&#8217;</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="color: #009900;">}</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="color: #009900;">}</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="font-weight: bold; color: #000066;">catch</span><span style="color: #009900;">(</span>e<span style="color: #009900;">)</span><span style="color: #009900;">{</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;">request<span style="color: #339933;">=</span><span style="font-weight: bold; color: #003366;">false</span><span style="color: #339933;">;</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="color: #009900;">}</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="font-weight: bold; color: #000066;">return</span> request<span style="color: #339933;">;</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="color: #009900;">}</span></div>
</li>
</ol>
</div>
<p><span id="more-81"></span>其中有些东西是参考别人的文章。<br />
第二步：做一个评论的HTML页，这个就不在这里弄出来。<br />
第三步：再写JS函数，把评论数据拿出来，发到服务器上的PHP的文件。<br />
我用的POST方法，当时做的时候，总是有问题，可是在网上百度或是GOOGLE，大部分都是GET方法，而且转载的有多少就更不想说了。</p>
<div style="border: 1px dashed #cccccc; font-weight: normal; font-size: 12px; line-height: 18px; font-style: normal; font-variant: normal;">
<div style="border-bottom: 1px dashed #cccccc; padding: 5px 10px; font-weight: bold; line-height: 20px; height: 20px;">JAVASCIPT</div>
<ol>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;">ajax.<span style="color: #000066;">open</span><span style="color: #009900;">(</span><span style="color: #3366cc;">&#8216;POST&#8217;</span><span style="color: #339933;">,</span>url<span style="color: #339933;">,</span><span style="font-weight: bold; color: #003366;">true</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;">ajax.<span style="color: #660066;">setRequestHeader</span><span style="color: #009900;">(</span><span style="color: #3366cc;">&#8216;Content-type&#8217;</span><span style="color: #339933;">,</span><span style="color: #3366cc;">&#8216;application/x-www-form-urlencoded&#8217;</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;">ajax.<span style="color: #660066;">send</span><span style="color: #009900;">(</span>querystr<span style="color: #009900;">)</span><span style="color: #339933;">;</span></div>
</li>
</ol>
</div>
<p>用POST方法，发送数据时，在send之前要加入<span style="color: #660066;">setRequestHeader</span><span style="color: #009900;">(</span><span style="color: #3366cc;">&#8216;Content-type&#8217;</span><span style="color: #339933;">,</span><span style="color: #3366cc;">&#8216;application/x-www-form-urlencoded&#8217;</span><span style="color: #009900;">), </span> 其中<span style="color: #ff6600;">大小写一定要注意</span> ，open方法的url自己指定要发送的PHP文件，send里的querystr，就像平时的GET方法传值一样拼接一下，中间的加的这一句就是让querystr以表单形式发送出去，这里就涉及到了编码问题。不加这一句，PHP文件通过$_POST是接收不到值的。<br />
第四步：写PHP文件，接收并处理$_POST数据。这里也没什么难的，如果你写过PHP程序并用过$_POST方法传值，这一点问题也没有。只是数据输 出的方式要注意。最开始接触AJAX的时候，只是看书，而且书上大多写的是如何创建XMLHttpRequest对象，很少讲到后台ASP/PHP怎么样 处理并返回数据。<br />
对于和我一样有这种意识的PHP新手，我会简单说明下，你一下就明白了。AJAX接收的PHP返回的数据，其实很简单比如</p>
<div style="border: 1px dashed #cccccc; font-weight: normal; font-size: 12px; line-height: 18px; font-style: normal; font-variant: normal;">
<div style="border-bottom: 1px dashed #cccccc; padding: 5px 10px; font-weight: bold; line-height: 20px; height: 20px;">PHP</div>
<ol>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="color: #990000;">echo</span> <span style="color: #0000ff;">&#8216;Hi,This is the result!&#8217;</span><span style="color: #339933;">;<br />
这个就是PHP返回的数据。<br />
</span></div>
</li>
</ol>
</div>
<p>心想怎么echo ，的确，JS端这时候用responseText就可以接收到这样的输出，一般我们见的多的就是echo输出是输出到浏览器，实际上就AJAX接收的数据就是这样的。如果你不懂，就不要问，因为这样做是可行的。<br />
现在回归正题，我返回的是XML的数据，对这种格式用的不多，所以在我的程序里，也是很简单的应用，一般有两种方法，PHP调用XMLDocument， 还有就是直接用echo输出构建好的XML格式的数据。我用的是后一种方法。其中，返回XML数据时，要在你的PHP文件头部加入</p>
<div style="border: 1px dashed #cccccc; font-weight: normal; font-size: 12px; line-height: 18px; font-style: normal; font-variant: normal;">
<div style="border-bottom: 1px dashed #cccccc; padding: 5px 10px; font-weight: bold; line-height: 20px; height: 20px;">PHP</div>
<ol>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="color: #990000;">header</span><span style="color: #009900;">(</span><span style="color: #0000ff;">&#8216;content-type:text/xml;charset:utf-8&#8242;</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;"><span style="color: #990000;">header</span> <span style="color: #009900;">(</span><span style="color: #0000ff;">&#8220;Cache-Control: no-cache,must-revalidate&#8221;</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span></div>
</li>
</ol>
</div>
<p>如果不加入第一行，IE就接收不到，FF正常，我的测试。第二行，是强制禁止缓存，确保JS接收到的最新数据，理论，没有测试。</p>
<p>第五步：接收PHP返回的数据，并显示操作结果。因为返回的是XML的数据，var xmldoc=ajax.responseXML.documentElement;这样得到的就是一个XML文档对象，你可以像操作DOM一样操作他， 得到想要的数据，你问怎么操作？你会用DOM吗？不会就先把DOM学会，因为这是基础。这时候有了数据，那我就要发表评论了，按照我的评论的格式，依次创 建 文本节点，元素节点，结元素节点添加属性。。。这些操作，不想写太多了，新手要是能做到这儿，证明下面的已经自己会做了。<br />
在为元素节点添加属性时，IE和FF的不同做法</p>
<div style="border: 1px dashed #cccccc; font-weight: normal; font-size: 12px; line-height: 18px; font-style: normal; font-variant: normal;">
<div style="border-bottom: 1px dashed #cccccc; padding: 5px 10px; font-weight: bold; line-height: 20px; height: 20px;">JAVASCRIPT</div>
<ol>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;">div1node.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">(</span><span style="color: #3366cc;">&#8220;class&#8221;</span><span style="color: #339933;">,</span><span style="color: #3366cc;">&#8220;lesscontent&#8221;</span><span style="color: #009900;">)</span><span style="color: #339933;">;//FF</span></div>
</li>
<li style="font-weight: normal; vertical-align: top;">
<div style="margin: 0px; padding: 0px; background: transparent none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: monospace; font-style: normal; font-variant: normal; font-weight: normal; font-size: 1em; line-height: 1.2em; font-size-adjust: none; font-stretch: normal; vertical-align: top;">div1node.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">(</span><span style="color: #3366cc;">&#8220;className&#8221;</span><span style="color: #339933;">,</span><span style="color: #3366cc;">&#8220;lesscontent&#8221;</span><span style="color: #009900;">)</span><span style="color: #339933;">;//IE</span></div>
</li>
</ol>
</div>
<p>我想强调的是，在AJAX的过程中，以前只知道CSS在不同的浏览器间有不兼容，现在发现JS,DOM的操作的不兼容也很严重。所以大家、特别是和我一样是新手的PHPer,就更要注意兼容性问题了。还有写JS的时候，千万要注意大小写的问题。</p>
<h4  class="entry-title">Related Posts</h4><ul class="related_post"><li><a href="http://blog.xiongchuan.org/jquery-callback-function-something.html" title="jQuery,callback|回调函数的问题">jQuery,callback|回调函数的问题</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://blog.xiongchuan.org/to-study-ajax-follow-me.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

