开发者

Ajax : Internet Explorer error

开发者 https://www.devze.com 2022-12-26 12:47 出处:网络
I am working on a login form. The Ajax code is as follows: /* ---------------------------- */ /* XMLHTTPRequest Enable */

I am working on a login form. The Ajax code is as follows:

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

The code works absolutely fine on google chrome and mozilla firefox. It just does not work on IE 5 and IE 6. I cannot figure out why?

Update: Implemented few alert boxes in the code. found out that the execution in Intern开发者_JAVA技巧et explorer does not go beyond

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

Is there some other syntax to get the form values?


You've got some extra spaces around your 'nocache' querystring attribute's "=" sign. Not sure how IE handles those. Probably replaces them with "%20"s, which is not likely the source of your error, but it's malformed nonetheless.

Could you inspect/debug your JavaScript with the IE Developer Toolbar to make sure all your objects are getting created properly?

Also see my comment about the request caching. Maybe you had a request cached that returned an empty response so it only appears that it is not working.

Try putting some alert()'s in your callback function to see if it ever gets there.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号