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 badges
Add a ment  | 

4 Answers 4

Reset to default 6

Have 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