什么是Ajax调用的寿命? [英] What is the life span of an ajax call?

查看:156
本文介绍了什么是Ajax调用的寿命?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有这个code在javascript:

 函数doAnAjaxCall(){
    VAR xhr1 =新XMLHtt prequest();
    xhr1.open(GET,/ myLink的,真正的);
    xhr1.onreadystatechange =功能(){
        如果(this.readyState == 4和&安培; this.status == 200){
            警报(我有一个回应!);
        }
    };
    xhr1.send(空);
}
 

和让code在servlet是:

 公共类RootServlet延伸的HttpServlet {
    公共无效的doGet(HttpServletRequest的REQ,HttpServletResponse的RESP)抛出IOException异常{
        resp.getWriter()写(这是怎么回事DOC?)。
        resp.setStatus(200);
    }
}
 

将xhr1仍然等待在readyState的新变化?或它尽快得到的第一反应关闭? 如果它仍然是开放的,将它导致内存泄漏/慢浏览器过了一段时间,积累了一些这些的? 我应该始终调用resp.getWriter()。关闭()在这个servlet code结束了吗?

和,最后,在jQuery的球迷在那里:

确实 $。阿贾克斯()表现得像 XMLHtt prequest()在这方面?

解决方案
  

将xhr1仍然等待在readyState的新变化?或者是,一旦它得到的第一反应关闭?如果它仍然是开放的,它会经过一段时间导致内存泄漏/慢的浏览器,并积累了一些这些的?

在幕后,它仍然是开放的。它(和内存占用),然而在web浏览器引擎的责任。它保持在具有每个域的最大限制反正池一定数量的连接。 MSIE例如有一个bug,导致漏液离开时,他们仍在运行,而用户卸载(关闭)的窗口。

  

我应该始终调用 resp.getWriter()。关闭()在这个servlet code结束了吗?

没有必要。该servletcontainer将强行关闭。自己关闭它只有prevents一些(越野车),code在响应链进一步写入到响应体的风险。有关更多详细信息,请参阅<一href="http://stackoverflow.com/questions/1829784/should-i-close-the-servlet-outputstream/1829823#1829823">this回答。

  

,最后是对jQuery的球迷在那里:确实 $阿贾克斯()表现得像 XMLHtt prequest( )在这方面?

它使用 XMLHtt prequest 在幕后(仅当浏览器支持,否则它是MSIE ActiveX对象)。它构建在每次调用一个新的。打开href="http://$c$c.jquery.com/jquery-latest.js" rel="nofollow"> unminified源$ C ​​$ C 的 jQuery.ajaxTransport(的功能,所有的AJAX处理code几乎是200 LOC,它涵盖了所有可能的浏览器特定的bug修复,你可以考虑一下。

let's say I have this code in javascript:

function doAnAjaxCall () {
    var xhr1 = new XMLHttpRequest();
    xhr1.open('GET', '/mylink', true);
    xhr1.onreadystatechange = function() {
        if (this.readyState == 4 && this.status==200) {
            alert("Hey! I got a response!");
        }
    };
    xhr1.send(null);
}

and let the code in the servlet be:

public class RootServlet extends HttpServlet {
    public void doGet (HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.getWriter().write("What's up doc?");
        resp.setStatus(200);
    }
}

Will xhr1 still wait for new changes in readystate? Or it is closed as soon as it gets the first response? If it remains open, will it lead to memory leaks/slower browser after a while and accumulating a few of those? Should I always call resp.getWriter().close() at the end of the servlet code?

And, lastly, for the jQuery fans out there:

does $.ajax() behave as XMLHttpRequest() in that respect?

解决方案

Will xhr1 still wait for new changes in readystate? Or it is closed as soon as it gets the first response? If it remains open, will it lead to memory leaks/slower browser after a while and accumulating a few of those?

Behind the scenes, it remains open. It (and the memory occupation) is however the responsibility of the webbrowser engine. It maintains a certain amount of connections in a pool which has a maximum limit per domain anyway. MSIE for example has a bug which causes them to leak away when they are still running while the user unloads (closes) the window.

Should I always call resp.getWriter().close() at the end of the servlet code?

Not necessary. The servletcontainer will close it anyway. Closing it yourself only prevents the risk of some (buggy) code further in the response chain from writing to the response body. For more detail, see this answer.

And, lastly, for the jQuery fans out there: does $.ajax() behave as XMLHttpRequest() in that respect?

It uses XMLHttpRequest under the covers (only when supported by the browser; otherwise it's the MSIE ActiveX object). It constructs a new one on every call. Open the unminified source code, Ctrl+F the jQuery.ajaxTransport( function. All the ajax handling code is almost 200 loc and it covers all possible browser specific bug fixes you can think about.

这篇关于什么是Ajax调用的寿命?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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