admin管理员组文章数量:1297131
I have some ajax queries that create & manipulate (external) DOM elements during different stages of the AJAX query (beforeSend, success, failure, plete). Multiple queries can be fired while others are still processing, and I'm wondering how to identify the DOM elements for each query to trigger the events for the correct one.
So, does jQuery .ajax provide access to a unique query identifier that I can parse into an ID for each respective DOM element?
$.ajax({
UNIQUE_ID_NEEDED_HERE = ??? # Need to get unique identifier for this AJAX query
url: '/my/query',
data: my_data,
dataType: "json",
beforeSend: function (response) {
$('#ajax_messages').append('<div class="loadingStatus" id="' + UNIQUE_ID_NEEDED_HERE + '">Re-ordering tasks</div>');
},
success: (message, text, response) {
$(UNIQUE_ID_NEEDED_HERE).attr('class', 'successfulStatus');
$(UNIQUE_ID_NEEDED_HERE).html('Tasks re-ordered');
}
});
If not, any alternative ideas appreciated.
I have some ajax queries that create & manipulate (external) DOM elements during different stages of the AJAX query (beforeSend, success, failure, plete). Multiple queries can be fired while others are still processing, and I'm wondering how to identify the DOM elements for each query to trigger the events for the correct one.
So, does jQuery .ajax provide access to a unique query identifier that I can parse into an ID for each respective DOM element?
$.ajax({
UNIQUE_ID_NEEDED_HERE = ??? # Need to get unique identifier for this AJAX query
url: '/my/query',
data: my_data,
dataType: "json",
beforeSend: function (response) {
$('#ajax_messages').append('<div class="loadingStatus" id="' + UNIQUE_ID_NEEDED_HERE + '">Re-ordering tasks</div>');
},
success: (message, text, response) {
$(UNIQUE_ID_NEEDED_HERE).attr('class', 'successfulStatus');
$(UNIQUE_ID_NEEDED_HERE).html('Tasks re-ordered');
}
});
If not, any alternative ideas appreciated.
Share Improve this question edited Aug 10, 2013 at 14:40 BenMorel 36.6k51 gold badges205 silver badges336 bronze badges asked Dec 25, 2011 at 10:21 PlankTonPlankTon 12.6k16 gold badges87 silver badges157 bronze badges4 Answers
Reset to default 6Have you tried something like this?
var constructRequest = (function() {
var startNumber = 0;
return function() {
var local = "request_id_"+(++startNumber);
$.ajax({
url: "someurl.php",
cache: false,
success: function(html){
/**
* Every time on success callback
* you will have unique local variable
* like this:
* request_id_1, request_id_2, request_id_3
* and so on.
***/
alert(local);
}
});
}
})();
$(document).ready(function() {
constructRequest();
constructRequest();
});
If I understand your purpose there's really no need to do this. All you need to do is create a standard "loading..." div template and clone it. store the clone in a variable and write it to the appropriate location in your document. When the AJAX success method fires it will do so inside the scope where it was created meaning you still have access to the same clone and can call any appropriate methods necessary on it.
Did you try:
function doAjaxMagic(idOfEl){
$.ajax
url: '/my/query'
data: my_data
dataType: "json"
beforeSend: (response) ->
$('#ajax_messages').append('<div class="loadingStatus" id="' + idOfEl + '">Re-ordering tasks</div>')
success: (message, text, response) ->
$("#"+idOfEl).attr('class', 'successfulStatus')
$("#"+idOfEl).html('Tasks re-ordered')
}
Is there a reason why that would not work?
Using a random number as the unique identifier greatly decreases the likelyhood of duplicating the identifier.
本文标签: javascriptJquery ajax() Unique AJAX query IdentifierStack Overflow
版权声明:本文标题:javascript - Jquery .ajax(): Unique AJAX query Identifier? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741617308a2388599.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论