Ajax调用不工作在IE8工作、Ajax

2023-09-10 17:26:49 作者:来一瓶82年的敌敌畏

我读一下这几款岗位,做出一些改变我的code,但没有运气。

I was reading several posts about this, and make some changes to my code but no luck.

任何人都可以看看这个,看看是怎么回事?或许另一种方法做什么,我需要(通过使用一个zip code检索城市,州ziptastic)

Can anyone look into this, to see what's going on here? Or perhaps another way to do what I need (retrieve city, state by a zip code using ziptastic)

在code正常工作中铬( http://jsfiddle.net/7VtHc/117/ )

The code works fine in 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>

谢谢

推荐答案

问题是IE8不支持的跨地资源共享(CORS) XHR,所以你不能做一个跨域AJAX调用本机XHR和jQuery的 $。阿贾克斯

The issue is IE8 doesn't support the Cross Origin Resource Sharing (CORS) XHR, so you can't do a cross domain ajax call with the native XHR or jQuery's $.ajax.

有关IE8,微软决定来使用CORS XHR,这就是所谓的 XDomainRequest ,所以你必须要落实,要支持IE8的用户。使用示例可以在this回答。

For IE8, Microsoft decided to come up with their own cross domain XHR instead of using the CORS XHR, which is called XDomainRequest, so you'll have to implement that to support IE8 users. An example usage can be found in this answer.

另外,你可以通过代理服务器的本地服务器端的跨域请求,使外部请求一个服务器到服务器的情况下,这将不会受到的同源策略。

Alternatively, you could proxy the cross domain request through your local server side, making the external request a server to server situation, which won't be subject to Same Origin Policy.