IE not reading GET success after POST

2019-09-13 11:46发布

问题:

I cannot seem to find out why IE does not read my success on get after the post. I have tried cache: false, with no luck. This works in all other browsers, just not IE.

       $.ajaxSetup({ cache: false });
        num = $('#num').val();
        phone = $('#phone').val();

        $.post("post.php?"+$("#MYFORM").serialize(), {

        }, function(response){

            if(response==1 && codeVal == 1 && telVal == 1)
            {
                $("#after_submit").html('');
                $("#Send").after('<label class="success" id="after_submit">Η αποστολή πραγματοποιήθηκε</label>');
                change_captcha();
                clear_form();
                $.ajax({
                        type:'get',
                        cache: false,
                        url: "http://web.somesite/submit_code.php",
                        dataType: 'html',
                        data:{ user: "one", pass: "mtwo", source: "WEB", receipt: num, msisdn: phone},
                        success: function(data) { 
                            var qsFull = "http://web.somesite.gr/submit_code.php?" + data;
                            var qs = URI(qsFull).query(true);
                            TINY.box.show({html:qs.message,animate:false,boxid:'error',top:5});
                        }
                    });
            }
            else
            {
                $("#after_submit").html('');
                $("#Send").after('<label class="error" id="after_submit">Error! in CAPTCHA .</label>');
            }
        });

OK, I tried adding an error after the success and I see that I get my pop up as I should be, but the value of qs.message is 0. Why would I get error and not success, when it is successful in other browsers.

回答1:

I found the answer, It has to do with IE not being flexible with cross domains and such, so I added a XDomainRequest like so

    if (jQuery.browser.msie && window.XDomainRequest) {
                           var xdr = new XDomainRequest();
                           var my_request_data = { user: "M1web", pass: "m!", source: "WEB", receipt: num, msisdn: phone};
                           my_request_data = $.param(my_request_data);



                                if (xdr) {
                                    xdr.onerror = function () {
                                       alert('xdr onerror');
                                    };
                                    xdr.ontimeout = function () {
                                       alert('xdr ontimeout');
                                    };
                                    xdr.onprogress = function () {
                                       alert("XDR onprogress");
                                    alert("Got: " + xdr.responseText);
                                    };
                                    xdr.onload = function() {
                                        //alert('onload  ' + xdr.responseText);
                                        var qsFull = "http://web.web.gr/submit_code.php?" + xdr.responseText;
                                        var qs = URI(qsFull).query(true);
                                        TINY.box.show({html:qs.message,animate:false,boxid:'error',top:5});
                                        callback(xdr.responseText);
                                    };
                                    xdr.timeout = 5000;
                                    xdr.open("get", "http://web.web.gr/submit_code.php?" + my_request_data);
                                    xdr.send();
                                } else {

                                }
                 }


回答2:

I unfortunately had to do a crash course in legacy IE behavior, and this post was very helpful. Here are some other links to help those having to deal with these issues:

  • Microsoft's Documentation of their XDomainRequest object
  • An internal blog post covering some of XDomainRequest's idiosyncrasies

Here's a function I use as a fallback where necessary:

// This is necessary due to IE<10 having no support for CORS.
function fallbackXDR(callObj) {
    if (window.XDomainRequest) { 
        var xdrObj = new XDomainRequest();
        xdrObj.timeout = callObj.timeout;
        xdrObj.onload = function() {
            handleSuccess(xdrObj.responseText);
        };
        xdrObj.onerror = function() {
            handleError(xdrObj);
        };
        xdrObj.ontimeout = function() {
            callObj.xdrAttempts = callObj.xdrAttempts++ || 1;
            if (callObj.xdrAttempts < callObj.maxAttempts) {
                fallbackXDR(callObj);
            }
        };
        xdrObj.onprogress = function() {
            // Unfortunately this has to be included or it will not work in some cases.
        };

        // Use something other than $.param() to format the url if not using jQuery.
        var callStr = callObj ? '?'+$.param(callObj.urlVars) : '';
        xdrObj.open("get", callObj.url+callStr);
        xdrObj.send();
    } else {
        handleError("No XDomainRequest available.", callObj);
    }
}//fallbackXDR()