0

This code works in every other browser EXCEPT IE8 on an XP machine. Can not for the life of me figure it out. In IE8 it will always display the error function. I have tried changing the dataType to jsonp, text, html, and no matter what it always pops the error function. Like I said, Chrome, Safari, Firefox, and all other IE's work, just not IE8.

<script type="text/javascript">
    $("#zip").submit(function (e) {
        e.preventDefault();
        if ($(this).parsley('isValid') === true) {
            var el = $("#zipcode");
            if ((el.val().length == 5) && (is_int(el.val()))) {
                $.ajax({
                    url: "http://zip.elevenbasetwo.com/v2/US/" + el.val(),
                    cache: false,
                    dataType: "json",
                    type: "GET",

                    success: function (result, success) {
                        console.log(result.state);
                        $('.rates-zip').fadeOut(function () {
                            switch (result.state) {
                            case "California":
                                $('#western').fadeIn();
                                break;
                            case "Illinois":
                            case "Virginia":
                                $('#midwest').fadeIn();
                                break;
                            case "New York":
                            case "New Jersey":
                                $('#eastern').fadeIn();
                                break;
                            case "Washington":
                                $('#northwest').fadeIn();
                                break;
                            default:
                                $('#default').fadeIn();
                            }
                        })



                    },
                    error: function (result, success) {
                        alert("Error IE8");
                    }
                });
            }
        };

    });

    function is_int(value) {
        if ((parseFloat(value) == parseInt(value)) && !isNaN(value)) {
            return true;
        } else {
            return false;
        }
    }
</script>

Hoping someone might have a solution.

Greg
  • 2,163
  • 1
  • 21
  • 23
  • Examine action error. what does it say? – Yuriy Galanter Apr 04 '14 at 18:26
  • 1
    Is zip.elevenbasetwo.com your domain and what version of jQuery are you using? I don't think that IE8 supports CORS in the same way as other more modern browsers. – Cameron Tinker Apr 04 '14 at 18:30
  • You may be running into one of the situations where IE8 doesn't play nice with e.preventDefault(), see: http://stackoverflow.com/questions/21033728/jquery-preventdefault-and-ie8-clarification – Culyx Apr 04 '14 at 18:32
  • @CameronTinker you may be right. http://caniuse.com/cors. This article seems to go further deep into that, and also provides some workarounds: http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx – victorantunes Apr 04 '14 at 18:35
  • You need a jQuery plugin to add CORS to IE8. – epascarello Apr 04 '14 at 18:39
  • possible duplicate of [AJAX cross-domain request IE 8+](http://stackoverflow.com/questions/17550248/ajax-cross-domain-request-ie-8) – epascarello Apr 04 '14 at 18:40

1 Answers1

0

Check that you have enabled CORS in jQuery:

$.support.cors = true;

Also, make sure that you are running no later than jQuery 1.9.1 in IE8. Support for IE8 was dropped in jQuery 2.x.

You may need to dynamically insert the correct version of jQuery into your DOM based on the IE version.

Something like this could work:

var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
var ie = re.exec(navigator.userAgent)?parseFloat(RegExp.$1):10;

if(ie >= 10)
    document.write("<script src='/js/jquery2.1.0.js'><\/script>");
else
    document.write("<script src='/js/jquery1.9.1.js'><\/script>");

Also, with IE8, you need to use a plugin to enable CORS with XDomainRequest since it doesn't support XMLHTTPRequest. The jQuery team doesn't plan to add support for this to jQuery core since IE8 is a quickly aging browser.

Aside from all of this, your remote server needs to support CORS too. It needs to allow all origins through the Access-Control-Allow-Origin response header. If this header is not present in the response, it will not give back a 200 OK response.

Cameron Tinker
  • 9,634
  • 10
  • 46
  • 85