已被阿贾克斯在Firefox中正确实施?同步问题 [英] Has Ajax been implemented correctly in Firefox? Synchronous issue

查看:108
本文介绍了已被阿贾克斯在Firefox中正确实施?同步问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请看看下面的code,你认为哪些日志先打印?
在Chrome浏览器和IE浏览器,同步Ajax调用:成功是显示第一预计,
但在FF(测试在FF 3.6安培; FF 17.0),异步调用Ajax:成功首先表现相反,
这意味着,尽管我们做了第二个作为sychronous电话,但是当它的onreadystatechange被触发,台异步(第一)Ajax调用的处理程序执行earliar比sychronous(二)Ajax调用,是否有意义?
这难道不是一个Firefox的错误?

  //第一个Ajax调用,注意:这是异步的。
$阿贾克斯({
    网址:/ REST / someUrl
    异步:真正的,
    数据类型:JSON,
    的contentType:应用/ JSON
    成功:功能(数据){
        的console.log(异步调用Ajax:成功);
    },
    错误:功能(数据){
    }
})
//第二个Ajax调用,注意:这是同步的。
$阿贾克斯({
    网址:/ REST / someUrl
    异步:假的,
    数据类型:JSON,
    的contentType:应用/ JSON
    成功:功能(数据){
        的console.log(同步AJAX调用:成功);
    },
    错误:功能(数据){
    }
})
 

解决方案

要实现一些正确的,必须有一定的规范。

在规范中我还没有发现任何提及一个事实,即所有脚本应该停止只要同步请求未执行完毕(注意,异步XHR正在运行的同步XHR启动时)。

不过,我发现这一点:

  1. 每个XMLHtt prequest对象都有自己任务源。也就是说,XMLHtt prequest任务源。
       - 这两个请求重新present单任务源 -

  2. 当用户代理是要排队一个任务,它必须给定的任务添加到相关事件循环的任务队列之一。 [...]从不同任务源任务可被放置在不同的任务队列。
      - 这两个任务可以添加到同一个任务队列,但绝不能 -

  3. <一个href="http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#processing-model-4"相对=nofollow>一个事件循环必须通过只要它存在以下步骤连续地运行:
    1.运行的最早的任务上的事件循环的任务队列中的一个,如果有的话,[...]。用户代理可以选择的任何任务队列。
      - 他拿起现在的任务队列,他穿的同步请求

当我没有误解这一点,我的逻辑是没有错的,这可能是怎么回事:

Firefox让两个XHR在同一个队列,IE和Chrome将它们放到不同的任务队列。

所有浏览器上运行,现在的任务队列,他们把同步XHR。

  • 在IE浏览器和Chrome同步XHR是在他的队列和运行最早的任务
  • 在FF异步XHR是最古老的在他的队列和运行

这两种实现方式似乎是正确的。

Please take a look at the following code, do you think which log will be printed first?
In Chrome & IE, "sync ajax call: success" is showed first which is expected,
BUT in FF(tested in FF 3.6 & FF 17.0), "async ajax call: success" is showed first instead,
which means although we make the second one as a sychronous call, but when its onreadystatechange was triggered, the handler of asychronous(first) ajax call was executed earliar than that of sychronous(second) ajax call, does it make sense?
Isn't it a firefox bug?

// first ajax call, Note: this is asynchronous.
$.ajax({
    url: "/rest/someUrl",
    async : true,
    dataType : "json",
    contentType: "application/json",
    success : function(data) {
        console.log("async ajax call: success");
    },
    error : function(data) {
    }
})
// second ajax call, Note: this is synchronous.
$.ajax({
    url: "/rest/someUrl",
    async : false,
    dataType : "json",
    contentType: "application/json",
    success : function(data) {
        console.log("sync ajax call: success");
    },
    error : function(data) {
    }
})

解决方案

to implement something "correctly" , there must be some specification.

Inside the specification I haven't found any reference to the fact that all scripts should stop executing as long as the synchronous request isn't finished(note that the async-XHR is already running when the sync-XHR starts).

But I found this:

  1. Each XMLHttpRequest object has its own task source. Namely, the XMLHttpRequest task source.
    --both requests represent a single task-source--

  2. When a user agent is to queue a task, it must add the given task to one of the task queues of the relevant event loop. [...] tasks from different task sources may be placed in different task queues.
    --both tasks may be added to the same task-queue, but must not--

  3. An event loop must continually run through the following steps for as long as it exists:
    1.Run the oldest task on one of the event loop's task queues, if any, [...]. The user agent may pick any task queue.

    --he picks now the task-queue where he puts on the synchronous request

When I not misunderstood this, and my logic is not wrong, this could be going on:

Firefox puts both XHR on the same queue, IE and chrome put them onto different task-queues.

All browsers run now the task-queue where they placed the synchronous XHR.

  • In IE and chrome the synchronous XHR is the oldest task in his queue and runs
  • In FF the asynchronous XHR is the oldest in his queue and runs

Both implementations seem to be correct.

这篇关于已被阿贾克斯在Firefox中正确实施?同步问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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