Som*_*ody 4 javascript asp.net ajax jquery internet-explorer-8
我正在阅读有关此内容的几篇帖子,并对我的代码进行了一些更改,但没有运气.
任何人都可以看看这个,看看这里发生了什么?或者也许是另一种方式来做我需要的事情(使用ziptastic检索城市,邮政编码状态)
代码在Chrome(http://jsfiddle.net/7VtHc/117/)中正常工作
HTML
<asp:TextBox ID="txtZipCode" runat="server"></asp:TextBox>        
<asp:TextBox ID="txtCity" runat="server"></asp:TextBox> 
<asp:TextBox ID="txtState" runat="server"></asp:TextBox> 
脚本
<script src="http://code.jquery.com/jquery-1.10.2.js" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        $("input[id$='txtZipCode']").keyup(function () {
            var el = $(this);
            if (el.val().length === 5) {
                $.ajax({
                    url: "http://zip.getziptastic.com/v2/US/" + el.val(),
                    cache: false,
                    dataType: "json",
                    type: "GET",
                    success: function (result, success) {
                        $("input[id$='txtCity']").val(result.city);
                        $("input[id$='txtState']").val(result.state);
                    }
                });
            }
        });
    });
</script>
谢谢,
您的代码中的实际问题是在ajax错误回调中显示"No Transport"错误.添加此行jQuery.support.cors = true ; 会解决你的问题
在IE和其他浏览器中尝试以下代码.
    <html>
    <head>
    <script type='text/javascript' src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
          <script>
    $(document).ready(function() {
        jQuery.support.cors = true;
        $("#zip").keyup(function() {
            var el = $(this);
            if (el.val().length === 5) {
               $.ajax({
                    url: "http://zip.getziptastic.com/v2/US/" + el.val(),
                    cache: false,
                    dataType: "json",
                    type: "GET",
                    success: function(result, success) {
                        $("#city").val(result.city);
                        $("#state").val(result.state);
                    }
                });
            }
        });
    });
    </script>
    </head>
    <body>
        <p>Zip Code: <input type="text" id="zip" /></p>
        <p>City: <input type="text" id="city" /></p>
        <p>State: <input type="text" id="state" /></p>
    </body>
    </html>
看看这些链接:
问题是IE8不支持跨源资源共享(CORS) XHR,因此您无法使用本机XHR或jQuery进行跨域ajax调用$.ajax.
对于IE8,微软决定提出他们自己的跨域XHR,而不是使用名为XDomainRequest的CORS XHR ,因此你必须实现它以支持IE8用户.在这个答案中可以找到一个示例用法.
或者,您可以通过本地服务器端代理跨域请求,使外部请求成为服务器到服务器的情况,这将不受同源策略的约束.