简体   繁体   中英

IE9 + IE10 AJAX Call not working

I'm getting reports that a website I developed is not functioning as it should in IE 9 and IE 10. The problem occurs when attempting to submit a form:

$("form[name='signIn']").submit(function(e) {

    var formData = new FormData($(this)[0]);

    e.preventDefault();

    $( "#return_status_sign_in" ).empty();


    $.ajax({
        url: "<?= SITE_URL ?>account/login",
        type: "POST",
        data: formData,
        async: false,
        success: function (msg) {
            $('#return_status_sign_in').append(msg);
        },
        cache: false,
        contentType: false,
        processData: false
    });

});

The above submits the form via AJAX in all other browsers and works perfectly. However, in IE 9 and 10, the page refreshes and the POST data appears as get variables in the URL. How come is this happening? Could it be that e.preventDefault(); is not triggering? If so, what's the alternative to that?

As I stated in my comment, IE 9 uses the 'xdomainrequest' object to make cross domain requests and 'xmlhttprequest' for other requests. Below is a sample of code that I use to work around this issue. 'xdomainrequests' only send 'plain/text.' They cannot send JSON:

if ('XDomainRequest' in window && window.XDomainRequest !== null) {
    var xdr = new XDomainRequest(),
        data = JSON.stringify(jsonData);

    xdr.open('POST', 'http://www.yourendpoint.com');
    xdr.onload = function() {
        // When data is recieved
    };
    // All of this below have to be present
    xdr.onprogress = function() {};
    xdr.ontimeout = function() {};
    xdr.onerror = function() {};

    // Send the request. If you do a post, this is how you send data
    xdr.send(data);
} else {
    $.ajax({
            url: 'http://www.yourendpoint.com',
            type: 'POST',
            dataType: 'json',
            data: {
                // your data to send
            },
            cache: true
        })
        .done(function(data) {
            // When done
        })
        .fail(function(data) {
           // When fail 
        });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM