【问题标题】:Microsoft Edge easyXDM on("message") event not being called未调用 Microsoft Edge easyXDM on("message") 事件
【发布时间】:2016-03-30 06:55:54
【问题描述】:

在 Microsoft Edge 中,GET 请求未运行。我已经将代码单步执行到运行 AJAX 请求的位置,并在回调中设置断点。但是,代码永远不会到达回调。

我已经有一个带有回调的 .then() 和 .fail() 设置,并尝试添加带有回调的 .done() 和 .always(),但是回调中的代码都没有运行。

然后我检查了开发工具中的网络选项卡,我根本找不到请求。似乎 Edge 出于某种原因没有触发请求。

request = function(options, resolveScope) {
    var deferred = $.Deferred();
    corsHandler.makeRequest(options)
        .done(this._wrap(function(response) {
            deferred.resolveWith(resolveScope, [response]); //never gets here
        }, this))
        .fail(this._wrap(function(response) {
            deferred.rejectWith(resolveScope, [response]); //never gets here
        }, this));
    return deferred;
}

这就是上面调用的请求函数。

ajaxFunc = function(data, scope) {
    return request({
        url: '/path/to/server',
        internalUrl: true,
        method: 'GET',
        datatype: 'json',
        data: data
    }, scope);
}

这是用于发出该请求的实现。

(function() {
    // set data var
    return ajaxFunc(data, self)
        .then(function(res) { console.log(res); }) //never gets here
        .done(function(res) { console.log(res); }) //never gets here
        .fail(function(res) { console.log(res); }) //never gets here
        .finally(function(res) { console.log(res); }) //never gets here
 })();

这是cors的东西。 (我对此了解不多。)

 corsHandler.makeRequest = function(options) {
        // resolve default options
        _.defaults(options, {
            xhr:        null,
            corsUrl:    null,
            url:        null,
            method:     'GET',
            data:       {},
            success:    function() {},
            error:      function() {},
            terminate:  false,
            binary:     false,
            headers:    {},
            internalUrl: false,
            datatype: ''
        });
        // if url is internal, create absolute url from relative url
        if (options.internalUrl) {
            options.url = this.createAbsoluteInternalUrl(options.url);
        }

        // resolve cors url or proxy url
        options.corsUrl = options.corsUrl || this.getCorsUrl(options.url);
        if (!options.corsUrl) {
            options.url     = this.getProxyUrl(options.url);
            options.corsUrl = this.getCorsUrl(options.url);
        }

        // create xhr
        if (!options.xhr && options.corsUrl) {
            options.xhr = this.createXhr(options.corsUrl);
        }

        // create cleanup procedure
        var cleanUpAfterRequest = $.proxy(function() {
            if (options.terminate) {
                options.xhr.destroy();
                this._removeCacheXhr(options.corsUrl);
            }
        }, this);

        // prepare deffered object
        var deferred = $.Deferred();
        deferred
            .done(function() {
                if (options.success) {
                   options.success.apply(null, Array.prototype.slice.call(arguments));
                }
            })
            .fail(function() {
                if (options.error) {
                    options.error.apply(null, Array.prototype.slice.call(arguments));
                }
            });

        // make actual request
        if (!options.xhr) {
            throw 'corsHandler: xhr object was not created or defined to make request'; 
            // this does not happen
        }
        options.xhr.request(
            {
                url:    options.url,
                method: options.method,
                data:   options.data,
                binary: options.binary,
                headers: options.headers,
                datatype: options.datatype
            },
            function() {
                deferred.resolve.apply(null, Array.prototype.slice.call(arguments));
                cleanUpAfterRequest();
            },
            function() {
                deferred.reject.apply(null, Array.prototype.slice.call(arguments));
                cleanUpAfterRequest();
            }
        );
        return deferred;
    }

更新

看起来问题出在easyXDM。 waitForReady() 没有在边缘触发 on(window, "message", waitForReady)。我现在正在进一步研究这个问题。

easyXDM sn-p:

    targetOrigin = getLocation(config.remote);
    if (config.isHost) {
        // add the event handler for listening
        var waitForReady = function(event){
            if (event.data == config.channel + "-ready") {
                // replace the eventlistener
                callerWindow = ("postMessage" in frame.contentWindow) ? frame.contentWindow : frame.contentWindow.document;
                un(window, "message", waitForReady);
                on(window, "message", _window_onMessage);
                setTimeout(function(){
                    pub.up.callback(true);
                }, 0);
            }
        };
        on(window, "message", waitForReady);

        // set up the iframe
        apply(config.props, {
            src: appendQueryParameters(config.remote, {
                xdm_e: getLocation(location.href),
                xdm_c: config.channel,
                xdm_p: 1 // 1 = PostMessage
            }),
            name: IFRAME_PREFIX + config.channel + "_provider"
        });
        frame = createFrame(config);
    }

上面的 sn-p 运行了,但是 waitForReady 方法永远不会被调用。唯一没有调用它的浏览器是 Edge(适用于 IE8+、Chrome、Safari、FF 和移动版 chrome/safari)。

【问题讨论】:

  • 属性不是dataType不是datatype吗?不要认为这会是问题,但值得纠正。过去我在犯该错误时遇到过请求未能触发
  • 将console.log 放在几乎所有有.done .fail .then 的地方,看看运行了什么。
  • @AntonioManente 感谢您的意见,我会调查的。
  • 啊,抱歉,这里打错了。我已经更新了。
  • 另外,有人对这个问题投了反对票,如果你要投反对票,至少评论一下你为什么投反对票。如果这是一个“愚蠢”的问题,那么答案也会很好......

标签: javascript ajax microsoft-edge easyxdm


【解决方案1】:

事实证明,以前的开发人员在我们的 easyXDM 实现中写入了一个必需的“hack”。

在我们的 easyXDM 实现中,我们必须在 IE 上更新 Window 对象,因为我们的应用程序在 iFrame 中启动。由于 Edge 在技术上不是 IE 版本,因此我们的测试失败了,因此代码没有运行以在 easyXDM 上下文中将 window 更新为 window.parent。

我们使用typeof document.documentMode === 'number' 来检测IE,但document.documentMode 在Edge 中未定义,因此我们添加了navigator.userAgent 对Edge 的检查。

这解决了问题。

【讨论】:

  • 这个 window / window.parent 修复了什么?由于各种原因,我在我的应用程序中检测到 Edge,但是在 easyXDM 的上下文中运行更新窗口为 window.parent 的这段代码是什么?参考?您的任何示例代码 sn-ps 中都没有 window.parent 代码。谢谢
  • 这个问题很老了,具体我不记得了,但是我们的代码在一个iFrame中,所以它可能是对iframe父窗口的引用。
猜你喜欢
  • 2018-10-05
  • 2016-03-18
  • 2020-06-26
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
  • 2016-12-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多