简体   繁体   English

ajax通话的寿命是多少?

[英]What is the life span of an ajax call?

let's say I have this code in javascript: 假设我在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: 并让servlet中的代码为:

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? xhr1还会等待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? 我应该总是在servlet代码的末尾调用resp.getWriter()。close()吗?

And, lastly, for the jQuery fans out there: 最后,对于那里的jQuery粉丝:

does $.ajax() behave as XMLHttpRequest() in that respect? $.ajax() XMLHttpRequest()在这方面表现为XMLHttpRequest()吗?

Will xhr1 still wait for new changes in readystate? xhr1还会等待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. 然而,它(以及内存占用)是webbrowser引擎的责任。 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. 例如,MSIE有一个错误,当用户卸载(关闭)窗口时,它们会在它们仍在运行时泄漏。

Should I always call resp.getWriter().close() at the end of the servlet code? 我应该总是在servlet代码的末尾调用resp.getWriter().close()吗?

Not necessary. 不必要。 The servletcontainer will close it anyway. 无论如何,servletcontainer都将关闭它。 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? 最后,对于那里的jQuery粉丝: $.ajax() XMLHttpRequest()在这方面是否表现为XMLHttpRequest()

It uses XMLHttpRequest under the covers (only when supported by the browser; otherwise it's the MSIE ActiveX object). 它使用XMLHttpRequest (仅当浏览器支持时;否则它是MSIE ActiveX对象)。 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. 打开未经授权的源代码 ,按Ctrl + F jQuery.ajaxTransport(函数。所有ajax处理代码几乎是200 loc,它涵盖了您可以考虑的所有可能的浏览器特定错误修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM