解決XMLHttpRequest在讀取xml資料時,不會refresh的問題
最近在小舖看到很多人在問這個問題...
小弟其實也不知道如何解....
到網路上找了一下資料...其實有解的...
會發生此問題有可能是瀏覽器cache住xml檔了
xmlhttprequestTest.aspx
01 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="xmlhttprequestTest.aspx.cs"
02 Inherits="xmlhttprequestTest" %>
03
04 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
05 <html xmlns="http://www.w3.org/1999/xhtml">
06 <head id="Head1" runat="server">
07 <title>xml</title>
08
09 <script type="text/javascript">
10 function show()
11 {
12 var http = new ActiveXObject("Microsoft.XMLHTTP");
13 http.open("GET", "XMLFile.xml", false );
14 //這行是關鍵
15 http.setRequestHeader('If-Modified-Since', 'Wed, 15 Nov 1995 00:00:00 GMT');
16 http.send(null);
17 var xmlDocument = http.responseXML;
18 alert(xmlDocument.getElementsByTagName('root')[0].firstChild.data);
19 }
20 </script>
21
22 </head>
23 <body>
24 <form id="form1" runat="server">
25 <div>
26 <input type="button" value="click me" onclick="show()" />
27 </div>
28 </form>
29 </body>
30 </html>
31
32 <script type="text/javascript">
33 show()
34 </script>
35
36
02 Inherits="xmlhttprequestTest" %>
03
04 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
05 <html xmlns="http://www.w3.org/1999/xhtml">
06 <head id="Head1" runat="server">
07 <title>xml</title>
08
09 <script type="text/javascript">
10 function show()
11 {
12 var http = new ActiveXObject("Microsoft.XMLHTTP");
13 http.open("GET", "XMLFile.xml", false );
14 //這行是關鍵
15 http.setRequestHeader('If-Modified-Since', 'Wed, 15 Nov 1995 00:00:00 GMT');
16 http.send(null);
17 var xmlDocument = http.responseXML;
18 alert(xmlDocument.getElementsByTagName('root')[0].firstChild.data);
19 }
20 </script>
21
22 </head>
23 <body>
24 <form id="form1" runat="server">
25 <div>
26 <input type="button" value="click me" onclick="show()" />
27 </div>
28 </form>
29 </body>
30 </html>
31
32 <script type="text/javascript">
33 show()
34 </script>
35
36
XMLFile.xml
1 <?xml version="1.0" encoding="utf-8" ?><xml> <root>BlueShop</root></xml>
參考網址:http://lists.apple.com/archives/dashboard-dev/2005/May/msg00249.html