作者danny0838 (道可道非常道)
看板Ajax
標題Re: [問題] 一個 JS 的系統問題
時間Thu Jan 15 10:59:58 2009
: → fillano:ajax可以在本機操作,但是ff3需要用about:config改權限 01/13 11:57
在下用 IE 在本機實測結果是不行,
Fx 不知道要改哪一個 config,不過如果 IE 不行的話,在下就不考慮了。
放上伺服器的話,IE 和 Fx 都測過可以。
測試檔:
HelloAjaxEx-1.txt
這是非同步請求的回應文字
HelloAjaxEx-1.js
var xmlHttp;
function createXMLHttpRequest() {
if(window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
else if(window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
function startRequest() {
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET", "HelloAjaxEx-1.txt");
xmlHttp.send(null);
}
function handleStateChange() {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
alert("伺服端回應:" + xmlHttp.responseText);
}
}
}
HelloAjaxEx-1.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=Big5" http-equiv="content-type">
<title>Hello! Ajax! Examples...</title>
<script type="text/javascript" src="HelloAjaxEx-1.js"></script>
</head>
<body>
<center><
input value="Ajax請求" onclick="startRequest();"
type="button"></center>
</body>
</html>
(↑按下綠色的按鈕執行)
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 163.25.118.142
1F:→ fillano:ie會阻擋所有本機執行的html檔的javascript,不只是ajax 01/15 11:29
2F:→ fillano:簡單繞過的方法:把副檔名改成hta即可 01/15 11:30
3F:→ danny0838:IE 是會擋 js,但是按下允許就好了呀... 01/15 11:37
4F:→ danny0838:副檔名改成 hta 試過,還是在 xmlHttp.open 被拒 01/15 11:38
5F:→ fillano:把status==200判斷拿掉試試看。access本地檔案,永遠是0喔 01/15 15:32
6F:→ danny0838:拿掉了還是不行... 01/15 21:03
7F:→ fillano:先用Microsoft.XMLHTTP,而不是XmlHttpRequest。 01/15 22:47
8F:→ fillano:看起來你是用ie7來做測試的。 01/15 22:48
9F:→ danny0838:是用IE7...成功了,謝謝您 01/15 23:05