<head>
<title>首頁新聞測(cè)試</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script type="text/javascript" src="xmlhttprequest.js"></script>
</head>
<body>
<div id="News">新聞讀取中……</div>
<script type="text/javascript">
var req = new XMLHttpRequest();
if( req )
{
req.onreadystatechange = function()
{
var News = "新聞讀取失敗";
try
{
if( req.readyState == 4 && ( req.status == 200 || req.status == 304 ) )
News = req.responseText;
}
catch(e)
{
News = e.description;
}
document.getElementById( ‘News‘ ).innerHTML = News;
};
req.open( ‘GET‘, ‘news.php‘ );
req.send( null );
}
</script>
</body>
</html>
<?php
echo iconv( "GB2312", "UTF-8", "這就是新聞" );
//echo "這就是新聞";
?>
如果注釋掉第一句而開放第二句的話不僅看到的是亂碼,在 IE 下還會(huì)出現(xiàn) -1072896748 錯(cuò)誤。有意思的是 IE 好像還會(huì)緩存由 XMLHTTPRequest 收到的內(nèi)容,而 FireFox 則不會(huì),所以在測(cè)試中會(huì)出現(xiàn)刷新 IE 很多次看到的還是舊數(shù)據(jù)的問題,可以在程序里加上下面 4 句話解決這個(gè)問題:
header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header( "Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . " GMT" );
header( "Cache-Control: no-cache, must-revalidate" );
header( "Pragma: no-cache" );
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=547782




