我正在使用登錄表單。 Ajax代碼如下:Ajax:Internet Explorer錯誤
/* ---------------------------- */
/* XMLHTTPRequest Enable */
/* ---------------------------- */
function createObject()
{
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}else{
request_type = new XMLHttpRequest();
}
return request_type;
}
var http = createObject();
/* -------------------------- */
/* LOGIN */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */
var nocache = 0;
function login() {
// Optional: Show a waiting message in the layer with ID ajax_response
document.getElementById('login_response').innerHTML = "<img src='images/ispinner.gif'/>"
// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
var email = encodeURI(document.getElementById('emailLogin').value);
var psw = encodeURI(document.getElementById('pswLogin').value);
// Set te random number to add to URL request
nocache = Math.random();
// Pass the login variables like URL variable
http.open('get', 'login.php?email='+email+'&psw='+psw+'&nocache = '+nocache);
http.onreadystatechange = loginReply;
if(window.XMLHttpRequest)
{
http.send(null);
}
else
{
http.send()
}
}
function loginReply() {
if(http.readyState == 4){
var response = http.responseText;
document.getElementById('login_response').innerHTML = response;
}
}
該代碼在谷歌瀏覽器和Mozilla Firefox中絕對正常。它只是不適用於IE 5和IE 6.我不明白爲什麼?
更新:代碼中實現了少數警報框。發現在Internet Explorer中的執行不超出
document.getElementById('login_response').innerHTML = "<img src='images/ispinner.gif'/>"
是否有一些其他語法來獲取表單值?
你應該使用jQuery。它會讓你的代碼更簡單。 – SLaks 2010-04-09 14:57:01
Math.Random()是確保您的請求無法緩存的不佳方法。我會將其更改爲時間戳。 'var nocache =(new Date())。getTime()。toString();'。我第二次SLak評論,爲你的AJAX使用一個庫。 http://api.jquery.com/jQuery.get/ – 2010-04-09 15:02:06
你如何稱呼你的登錄功能? – Gregoire 2010-04-09 15:22:44