簡體   English   中英

Ajax:Internet Explorer錯誤

[英]Ajax : Internet Explorer error

我正在使用登錄表單。 Ajax代碼如下:

/* ---------------------------- */
/* 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;
    }
    }

該代碼在google chrome和mozilla firefox上絕對可以正常工作。 它只是在IE 5和IE 6上不起作用。我不知道為什么嗎?

更新:在代碼中實現了幾個警報框。 發現Internet Explorer中的執行沒有超出范圍

document.getElementById('login_response').innerHTML = "<img src='images/ispinner.gif'/>"

還有其他語法可以獲取表單值嗎?

您在'nocache'查詢字符串屬性的“ =”符號周圍有一些多余的空格。 不確定IE如何處理這些問題。 可能將它們替換為“%20”,這不太可能是錯誤的根源,但它的格式仍然不正確。

您是否可以使用IE開發人員工具欄檢查/調試JavaScript,以確保正確創建了所有對象?

另請參閱我對請求緩存的評論。 也許您緩存了一個請求,該請求返回了一個空響應,因此它似乎僅在不起作用。

嘗試在您的回調函數中放入一些alert(),看它是否到達那里。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM