oneiros
oneiros

Reputation: 3568

Issue with Ajax POST in Firefox and Chrome but not IE8

I have a problem with the following code. When I run it in IE8, I get an alert when I have a successful return from the call.

This does not happen in Firefox and Chrome, i.e. I get no alert when running it there. Everything else works, except that it seems to me like the code section which is supposed to execute once the call is successful fails.

function stuffFile(file, wfid) {

    var xmlhttp = new XMLHttpRequest();

    if(window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }

    var url = "http://someotherserver.page.aspx";
    var params = "fileName=" + file + "&param11=" + wfid;
    xmlhttp.open("POST", url, true);

    //Send the proper header information along with the request
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", params.length);
    xmlhttp.setRequestHeader("Connection", "close");

    xmlhttp.onreadystatechange = function() {//Call a function when the state changes.
        //alert('onready');
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var response = jQuery.trim(xmlhttp.responseText);
            alert('response ' + response);
        }
    }
    xmlhttp.send(params);


}

Upvotes: 0

Views: 363

Answers (1)

cambraca
cambraca

Reputation: 27835

You're already using jQuery, you should use its AJAX capabilities. It takes care of creating the XMLHTTPRequest object and all the differences between different browsers, and does a lot of the stuff you are doing manually.

Upvotes: 1

Related Questions