<?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>xmlhttp</title>
	<atom:link href="https://www.aitaocui.cn/tag/253306/feed" rel="self" type="application/rss+xml" />
	<link>https://www.aitaocui.cn</link>
	<description>翡翠玉石爱好者聚集地</description>
	<lastBuildDate>Sun, 27 Nov 2022 21:53:20 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.1</generator>

<image>
	<url>https://www.aitaocui.cn/wp-content/uploads/2022/11/taocui.png</url>
	<title>xmlhttp</title>
	<link>https://www.aitaocui.cn</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>xmlhttp(浏览器对象)</title>
		<link>https://www.aitaocui.cn/article/365403.html</link>
					<comments>https://www.aitaocui.cn/article/365403.html#respond</comments>
		
		<dc:creator><![CDATA[和谐健康]]></dc:creator>
		<pubDate>Sun, 27 Nov 2022 21:53:20 +0000</pubDate>
				<category><![CDATA[知识]]></category>
		<category><![CDATA[xmlhttp]]></category>
		<guid isPermaLink="false">https://www.aitaocui.cn/?p=365403</guid>

					<description><![CDATA[Xmlhttp是一种浏览器对象，可用于模拟http的GET和POST请求。配合JavaScript可以实现页面数据在无刷新下的定时数据更新，如果应用在聊天室、文字直播上可以取得较好...]]></description>
										<content:encoded><![CDATA[</p>
<article>
<p>Xmlhttp是一种浏览器对象，可用于模拟http的GET和POST请求。配合JavaScript可以实现页面数据在无刷新下的定时数据更新，如果应用在聊天室、文字直播上可以取得较好的视觉效果。</p>
</article>
<article>
<h1>简介</h1>
<p>Xmlhttp是一种浏览器对象， 可用于模拟http的GET和POST请求。配合JavaScript可以实现页面数据在无刷新下的定时数据更新，如果应用在聊天室、文字直播上可以取得较好的视觉效果。 </p>
<p>IE中的 XmlHttp对象 </p>
<p>在IE中XmlHttp被实现为ActiveX对象，通常使用</p>
<p>var xmlhttp = new ActiveXObject(&quot;Msxml2.XMLHTTP&quot;); </p>
<p>来创建一个对象，然后使用该对象的open方法来发出一个Http请求。 </p>
<p>xmlhttp.open(&quot;GET&quot;, fragment_url); </p>
<p>这时候浏览器已经发出了Http请求，我们需要注册一个匿名函数给XmlHttp对象的onreadystatechange方法，这样当请求返回时，xmlhttp就会自动调用我们注册的这个函数，下边是一个实际的例子。 </p>
</p>
<h1>例子</h1>
<p>xmlhttp.onreadystatechange = function() </p>
</p>
<p>{ </p>
</p>
<p>if (xmlhttp.readyState == 4 &amp;&amp; xmlhttp.status == 200) </p>
</p>
<p>{ </p>
</p>
<p>element.innerHTML = xmlhttp.responseText; </p>
</p>
<p>} </p>
</p>
<p>} </p>
</p>
<p>因为我们不需要再发送任何信息，所以用下边的语句结束 </p>
</p>
<p>xmlhttp.send(null); </p>
</p>
<p>我们将上边的过程封装为一个函数，下边是这个函数的完整代码： </p>
</p>
<p>function loadFragmentInToElement(fragment_url, element_id) </p>
</p>
<p>{ </p>
</p>
<p>var element = document.getElementById(element_id); </p>
</p>
<p>var xmlhttp = new ActiveXObject(&quot;Msxml2.XMLHTTP&quot;); </p>
</p>
<p>xmlhttp.open(&quot;GET&quot;, fragment_url); </p>
</p>
<p>xmlhttp.onreadystatechange = function() </p>
</p>
<p>{ </p>
</p>
<p>if (xmlhttp.readyState == 4 &amp;&amp; xmlhttp.status == 200) </p>
</p>
<p>{ </p>
</p>
<p>element.innerHTML = xmlhttp.responseText; </p>
</p>
<p>} </p>
</p>
<p>} </p>
</p>
<p>xmlhttp.send(null);</p>
</p>
<p>PHPMORE VOL5 24/26 </p>
</p>
<p>} </p>
</p>
<p>函数的调用方法如下所示： </p>
</p>
<p>loadFragmentInToElement( http://domain.com/url.php, DynamicContent_id ); </p>
</p>
<p>有了上边的代码， 再配合JavaScript的定时函数， 我们就可以实现定时的无刷新数据更新了， 下边这个函数每隔5秒对element_id </p>
</p>
<p>的数据进行一次更新。 </p>
</p>
<p>function refresh( element_id ) </p>
</p>
<p>{ </p>
</p>
<p>loadFragmentInToElement( show.php , + element_id ); </p>
</p>
<p>setTimeout( &quot;refresh(ts)&quot; , 5000 ); </p>
</p>
<p>} </p>
</p>
<p>在 IE上使用XmlHttp要注意的问题 </p>
</p>
<p>特别要注意的是由于IE的Cache的关系，我们看见的XmlHttp并不总是最新读取的那一个，为了让IE不启用Cache，我们发送给 </p>
</p>
<p>IE一个特殊的Header，用PHP实现如下： </p>
</p>
<p>header( &quot;Expires: Mon, 26 Jul 1997 05:00:00 GMT&quot; ); </p>
</p>
<p>header( &quot;Last-Modified: &quot; . gmdate( &quot;D, d M Y H:i:s&quot; ) . &quot;GMT&quot; ); </p>
</p>
<p>header( &quot;Cache-Control: no-cache, must-revalidate&quot; ); </p>
</p>
<p>header( &quot;Pragma: no-cache&quot; ); </p>
</p>
<p>XmlHttp对象在Gecko上的实现 </p>
</p>
<p>Gecko上的XmlHttp和IE上略有不同，它并不需要通过ActiveX来创建。另外回调函数必须在open方法之前注册，而IE并不要 </p>
</p>
<p>求，这是一个很需要注意的问题。 </p>
</p>
<p>使用JavaScript实现XmlHttp的跨浏览器应用 </p>
</p>
<p>为了能在多种浏览器上有一个统一的实现， 我们可以用JavaScript来对不同浏览器的差异进行封装。 这里我们采用Andrew Gregory </p>
</p>
<p>的实现。首先我们要引用Andrew Gregory的一个名为xmlhttprequest.js的Js脚本。 </p>
</p>
<p>然后在创建XmlHttp对象时统一使用new XMLHttpRequest()就可以了； 其它的方法不用改变。 这个Js脚本运行我们在IE、 Gecko </p>
</p>
<p>（Mozilla/FireFox） 和Opera的特定版本使用XmlHttp。下边是调整后的loadFragmentInToElement函数， 这个函数在IE6 </p>
</p>
<p>和FireFox1.0pre上运行通过。 </p>
</p>
<p>xmlhttprequest.js文件和具体的使用例子可以在我写的一个DEMO中找到。 </p>
</p>
<p>XmlHttp中的中文乱码问题 </p>
</p>
<p>在默认情况下，XmlHttp都是使用Utf-8字符集，而我们使用的多是GB2312字符集，这就要求我们进行GB2312到Utf-8的转码。 </p>
</p>
<p>PHP提供了一个可选的专码模块，可以实现多种字符集之间的相互转化。加载这个专码模块的方法如下： </p>
</p>
<p>打开 PHP 配置文件 php.ini，将 ;extension=php_mbstring.dll（*nix 是 php_mbstring.so） 前的分号去掉。重新启动 </p>
</p>
<p>Apache以后，这个模块就可以使用了。如果有错误出现，请检查扩展目录的路径设置是否正确。 </p>
</p>
<p>加载这个模块以后，我们就可以使用mb_convert_encoding函数来转码了： </p>
</p>
<p>$utf8_string = mb_convert_encoding( $gb_string , UTF-8 , GB2312 ); </p>
</p>
<p>将转码后的字符输出就可以看见正确显示的中文</p>
</p>
</article>
<div class="mt-3 mb-3" style="max-width: 770px;height: auto;">
                                    </div>
<div class="mt-3 mb-3" style="max-width: 770px;height: auto;">
                                    </div>
<div class="mt-3 mb-3" style="max-width: 770px;height: auto;">
                                    </div>
]]></content:encoded>
					
					<wfw:commentRss>https://www.aitaocui.cn/article/365403.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
