-1

I have this code that pre-fills my website with user's city & state based off of a zip-code that is entered into my contact form.

It works perfect in Google Chrome, and Firefox, but it is not working in IE.

Does anyone know why, and if so, can you fix the code for me?

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<!--Realtime City/State Update From Zip Code Post-in-->
<script>
    $(document).ready(function () {
        var zip = $("#zip").val();
        $.getJSON('http://zip.elevenbasetwo.com/v2/US/' + zip, function (json) {
            $('#city').val(json.city);
        });
        $.getJSON('http://zip.elevenbasetwo.com?zip=' + zip, function (json) {
            $('#state').val(json.state);
        });
    });
</script>
<!--On Edit/Keyup City/State Update-->
<script>
    $(document).on("keyup", "#zip", function () {
        var zip = this.value;
        $.getJSON('http://zip.elevenbasetwo.com/v2/US/' + zip, function (json) {
            $('#city').val(json.city);
        });
        $.getJSON('http://zip.elevenbasetwo.com?zip=' + zip, function (json) {
            $('#state').val(json.state);
        });
    });
</script>

Edit
What I mean by not working is that on Google Chrome and Firefox my City & State fields in my contact form will automatically be pre-filled based on the zip-code that is entered.

However in IE (version 9 for me), the City & State fields are not pre-populating like they should.

Nope
  • 22,147
  • 7
  • 47
  • 72
Newbie
  • 25
  • 1
  • 6
  • 6
    Can you elaborate on what you mean 'it does not work'? Any error messages? – Hazem Salama Aug 14 '12 at 22:47
  • 4
    Disclaiming that you aren't interested in being told why it doesn't work does not make anyone want to help you. If you aren't interested in learning, we're going to see a lot more questions like this if we help. Remember, the way to stop being a newbie is to figure stuff out. – Winfield Trail Aug 14 '12 at 22:53
  • I never said I don't want to learn, I just wanted the solution so I can learn from that. Telling me a complicated fix won't help me. – Newbie Aug 14 '12 at 22:54
  • What is your IE version? JavaScript is enabled in your IE? – Yair Nevet Aug 14 '12 at 22:55
  • Version 9 as stated in the last paragraph, and yes JS enabled... – Newbie Aug 14 '12 at 22:55
  • Update your question with the full HTML please. – Yair Nevet Aug 14 '12 at 22:56
  • 1
    possible duplicate of [jQuery.getJSON not working properly in IE8 with gdata json-c. Why?](http://stackoverflow.com/questions/6318996/jquery-getjson-not-working-properly-in-ie8-with-gdata-json-c-why) – Dr.Molle Aug 14 '12 at 22:58

1 Answers1

0

Update

There's nothing wrong with the json. This is a cross-domain request issue with IE.

The general solution with jQuery would be to add jQuery.support.cors = true; at the top of your code. But this may not work.

IE8+ has window.XDomainRequest, which allows you to make cross-domain request.

Here's a jQuery plugin for it but I'm not sure how to make it work.

ajaxHooks xdr

Alternatively, you could get the information from a proxy on your server or using flash.

More information

Community
  • 1
  • 1
Larry Battle
  • 9,008
  • 4
  • 41
  • 55