-1

我有这个代码,它根据输入到我的联系表格中的邮政编码,用用户的城市和州预先填写我的网站。

它在 Google Chrome 和 Firefox 中完美运行,但在 IE 中无法运行。

有谁知道为什么,如果是的话,你能为我修复代码吗?

<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>

编辑
我的意思not working是,在 Google Chrome 和 Firefox 上,我的联系表单中的城市和州字段将根据输入的邮政编码自动预先填写。

但是在 IE(我的版本 9)中,City & State 字段并没有像应有的那样预先填充。

4

1 回答 1

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

于 2012-08-14T23:44:46.530 回答