作者mozan (想你的原因)
看板Ajax
標題[問題] 關於XMLHttpRequest 和 PHP
時間Mon Jul 23 14:09:31 2007
用如下的程式碼
我將他的目標網頁從test.html 改成我自己寫的 test.php
<span
style="cursor: pointer; text-decoration: underline"
onclick="makeRequest('
test.php')">
Make a request
</span>
test.php 則在資料庫中新增一筆資料 以及 一行 OK 的 echo
我直接連結那個php可以成功新增沒有問題
可是用XMLHttpRequest卻沒有成功(應該說是在IE沒有成功 可是firefox有)
在IE中有把OK alert出來但是沒有在資料庫新增
可是firefox有新增資料卻沒有alert出來
不知道是為什麼
http://wiki.moztw.org/index.php/AJAX_%E4%B8%8A%E6%89%8B%E7%AF%87
<script type="text/javascript" language="javascript">
var http_request = false;
function makeRequest(url) {
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
http_request.onreadystatechange = alertContents;
http_request.open('GET', url, true);
http_request.send(null);
}
function alertContents() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
alert(http_request.responseText);
} else {
alert('There was a problem with the request.');
}
}
}
</script>
<span
style="cursor: pointer; text-decoration: underline"
onclick="makeRequest('test.html')">
Make a request
</span>
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.123.101.247
※ 編輯: mozan 來自: 140.123.101.247 (07/23 14:10)
1F:推 Kej:IE 有 alert 但沒更新可能是 IE 的 cache 的關係,在 test.php 07/23 15:22
2F:→ Kej:後面加個會變的參數試試看,ex: test.php?t=<?=time()?> 07/23 15:23
3F:推 bryanliu:你的意思是直接在網址的URL後面+參數喔 但這樣資料庫 07/23 15:39
4F:→ bryanliu:還是沒有更新 07/23 15:41
5F:推 mozan:不太懂K大的意思 是直接丟一個參數給test.php echo嗎? 07/23 15:45
6F:推 averywu:丟你的php程式上來看看 07/23 16:02
7F:推 Kej:IE的XMLHTTP如果用GET Method讀取同一個url,只有第一次會真的 07/23 16:01
8F:→ Kej:去request,第二次之後他會看先前已經有request過了,就會直接 07/23 16:03
9F:→ Kej:讀cache,所以IE才只會新增一次,但是如果在request的url加上 07/23 16:03
10F:→ Kej:變數,讓每次去讀的url都不一樣的話,就不會有cache的問題 07/23 16:04
11F:→ mlwmlw:樓上一語驚醒 …我之前發生的問題原來這樣就能解決阿XD 07/23 18:38