How to pass parameter in response to jQuery ajax request?

I need to pass additional variables to the jQuery, ajax callback function.

For example, given:

while (K--)
{
    $.get
    (
        "BaseURL" + K,
        function (zData, K) {ProcessData (zData, K); }
    );
}

function ProcessData (zData, K)
{
    console.log (K);
}

ProcessData() will report 0 each time (or whatever the last K value is).

How can I guarantee that it ProcessData()works or can get the correct value of K?

Is there a way to do this without packaging $get()in a function?

+3
source share
1 answer

. - , , K ( 0). . :.

while (K--)
{
    (function(K)
    {
      $.get
      (
          "BaseURL" + K,
          function (zData, status) {ProcessData (zData, K); }
      );
    })(K);
}

, K. , K.

+5

Source: https://habr.com/ru/post/1753219/


All Articles