Are AJAX calls not blocking and what is their lifespan?
I feel ackward asking these fundamental questions given that I am not exactly new to web development. But I want to double-check my assumptions nevertheless...
I'm building the recording of unique image views in my application. When a user (not a bot) visits an image page, an Ajax call is made to a back-end process that collects the session info, compares for duplications and stores the visit. I have all my javascript references as well as this call at the bottom of the HTML, just before the </body>
element:
$.get(basepath + "image/1329/record/human", function(data){
console.log("Data Loaded: " + data);
});
By default, the call to $.get is made asynchronous. Yet I want to test the following assumptions:
- Is it correct that this method ensures that the call to the view recording script is non-blocking for the rest of开发者_开发百科 the UI?
- Is it correct that the back-end script will finish once called, regardless of whether the user navigates to another page?
According to jQuery .get reference...
This [$.get()] is a shorthand Ajax function, which is equivalent to:
$.ajax({ url: url, data: data,
success: success, dataType: dataType
});
And $.ajax is asynchronous (i.e. non-blocking) by default, that's what the A in Ajax means.
Also, back-end server code is started in the moment the server receives the request and then runs independently of the client staying on the page or not, unless you implement some kind of mechanism to stop the running service which I suppose you did not.
God bless!
The jQuery.get
request you're making is asynchronous and will not block the DOM or other JavaScript from continuing. The get
function is a shorthand method which uses jQuery.ajax.
The second question I don't have a solid answer for -- I expect it may depend more on how the back-end code is structured and whether it's told that the session/request has terminated.
API:
- jQuery.get()
- jQuery.ajax()
that is correct on both counts
精彩评论