0

因此,我使用 Backbone.js 编写了一个消息传递系统。它在 Chrome 和 FF 中运行良好,但 IE9 有一个特定的 fetch 调用问题会杀死它。(我在 MVC3 中工作)。

我有一个检查新消息的民意调查,它将日期发送到服务器。使用此方法调用民意调查:

DoMessageFetch = function() {
    var now = new Date().toUTCString();
            Chat.mymessages.fetch({
                cache: false,
                data: {
                    Now: now
                },
                success: function (response) {
                    // if there are messages ...
                    // for each message, open a chat window
                    if (Chat.mymessages.length > 0) {
                        for (var i = 0; i < Chat.mymessages.length; i++) {                            
                            var useridto = Chat.mymessages.at(i).get("UserId");
                            var name = Chat.mymessages.at(i).get("ScreenName");
                            // a chat-window with this useridto is NOT on the page
                            if (!($('#chat-window-' + useridto).is(':visible'))) {
                                Chat.doChatMessageFetch(name, useridto, null); // this constructs a Backbone view
                            }
                        }
                    }
                },
                error: function () { console.log('ERROR: fetching general poll messages failed.'); }
            });

            Chat.mymessages.reset();
}

在 IE9 中,当我在控制器中观察断点时,Now 参数为空。这意味着请求遵循服务器上错误的代码路径...

我不明白我的 Now 参数在 IE 中的位置。任何人都可以帮忙吗?

4

1 回答 1

1

这个问题是由于不同的行为

new Date().toUTCString()

在 IE、Google Chrome 和 Firefox 之间。

例如 Chrome 中的结果是:

"Thu, 20 Sep 2012 20:19:15 GMT" 

在 IE 中你会得到

"Thu, 20 Sep 2012 20:19:15 UTC" 

MVC3 ModelBinder 将忽略 IE 格式并将您的 Now 参数保留为空。解决此问题的方法是替换

new Date().toUTCString()

new Date().toJSON()

此解决方案唯一需要注意的是,由于缺少 toJSON() 函数,默认情况下它在 IE7 上不起作用,但可以使用 Douglas Crockford json2.js库解决此问题,在使用 Backbone.js 时也建议使用该库

于 2012-09-20T20:36:32.233 回答