IE9在jQuery.ajax上“抛出错误".方法 [英] IE9 throwing error on jQuery.ajax "done" method

查看:128
本文介绍了IE9在jQuery.ajax上“抛出错误".方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在最新的Chrome和Firefox中运行代码.
IE9却讨厌我(我也有同感).

I've got code working in the latest Chrome and Firefox.
IE9 however hates me (and I feel the same).

我遇到的具体错误是:
Object doesn't support property or method 'done'

The specific error I'm getting is:
Object doesn't support property or method 'done'

有问题的代码:

app.$.ajax({
                url: app.baseUrl + "Geos",
                contentType: "application/json",
                dataType: "json"
            }).done(function (data) {
                app.controls.ddlGeos.html('');

                for (var i = 0; i < data.d.length; i++) {
                    var geo = data.d[i];
                    var option = $(document.createElement("option"));

                    option.attr('value', geo.code);
                    option.html(geo.name);

                    app.controls.ddlGeos.append(option);
                };
            }).fail(function (xhr, status) {
                console.log("Error retrieving Geo list", xhr, status);
            }).always(function (data, status, xhr) {
                app.controls.ddlGeos.parent().find('#geo_ajax_image').remove();
                app.setSelectedValues(app.controls.ddlGeos, app.controls.hdnGeo.val());
            });

使这种情况略有异常的原因是我正在手动加载最新版本的jQuery 1.8.2),如下所示:

Something that makes this slightly abnormal is that I am manually loading the latest version of jQuery 1.8.2) like this:

// The following code allows us to run jQuery 1.8.2 independantly of the main jQuery
    if (!jq) {
        var script = document.createElement("script");
        script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js';
        script.onload = script.onreadystatechange = function () {
            window.jq18 = jQuery.noConflict(true);
            app.init(window.jq18);
        };
        document.body.appendChild(script);
    } else {
        app.init(jq);
    }

之所以这样做,是因为我正在针对一个非常老的Web应用程序编写并且无法覆盖window.$,因此我正在向我的应用程序发送最新的jQuery版本.
但是,我已经完成了一些控制台日志记录,并且在命中ajax调用时已加载了正确版本的jQuery IS. app.$().jquery输出"1.8.2"

The reason I am doing this is because I'm writing against a very old web app and I can't overwrite window.$ so I'm sending my app the newest jQuery version.
However, I have done some console logging and the correct version of jQuery IS being loaded by the time the ajax call has been hit. app.$().jquery outputs "1.8.2"

编辑
好的,我想我知道问题的一部分.我在2个不同的ASP.Net UserControls(想封装的网页)中做类似的代码.他们俩都以这种方式加载jQuery 1.8.2,因为两者都不能确定对方是否存在.看来Firefox和Chrome可以解决此问题,但是IE某种程度上导致了我的"onreadystatechange"事件触发了两次(并且其中一种在没有正确版本的jquery的情况下发生了). 我可以做些什么来修改该jquery加载代码,以确保它只发生一次?我这样称呼它:

EDIT
Ok, I think I know part of the problem. I'm doing similar code in 2 different ASP.Net UserControls (think encapsulated web page). They BOTH are loading jQuery 1.8.2 this way because neither can be sure the other exists. It seems like Firefox and Chrome handle this ok, but IE is somehow causing my "onreadystatechange" event to fire twice (and somehow one of them is happening without the proper version of jquery).
Is there anything I can do to modify that jquery loading code to ensure it only happens once? I'm calling it like this:

(function (jq) {
    // All my "app" code here
    app = {
        init : function($) {
            app.$ = $ || window.$;
            // Other code
        }
    };

    if (!jq) {
        var script = document.createElement("script");
        script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js';
        script.onload = script.onreadystatechange = function () {
            window.jq18 = jQuery.noConflict(true);
            app.init(window.jq18);
        };
        document.body.appendChild(script);
    } else {
        app.init(jq);
    }
})(window.jq18);

推荐答案

IE9似乎具有readyState,例如"uninitialized""loading""complete".这些之间的过渡似乎在IE中触发了onreadystatechange. onload最终也会被 锁定,因此您需要多次初始化所有内容.我不确定这是如何导致$.ajax奇怪的返回值的,但是显然删除它可以解决此问题.

IE9 seems to have readyStates such as "uninitialized", "loading" and "complete". The transitions between these seem to fire onreadystatechange in IE. The onload is eventually also fied, so you're initializing everything several times. I'm not sure how that causes the weird return value of $.ajax, but apparently removing it fixes it.

这篇关于IE9在jQuery.ajax上“抛出错误".方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆