loop of ajax calls - accessing loop counter?

后端 未结 1 647
借酒劲吻你
借酒劲吻你 2021-01-26 08:10

I am stuck here, any help would be appreciated.

I have a listbox of items, and I want to retrieve data on each item in the list via AJAX (which calls a webservice). The

相关标签:
1条回答
  • 2021-01-26 08:29

    Create a closure with a self-executing function:

    var NumRows = list.options.length;
    for ( var row = 0; row < NumRows; row ++ )
    {
      !function(row) {
          var Value = list.options[row].value;
          var xmlHttpObj = CreateXmlHttpRequestObject();
          if ( xmlHttpObj != null )
          {
            xmlHttpObj.open( "POST", "Async.ashx?arg1=GetPhysicalPathInfo&arg2=" + Value, true );
            xmlHttpObj.onreadystatechange = function ()
            {
              // code that needs to know what row we were from
            }
          }
          xmlHttpObj.send();
      }(row);
    }
    

    Of course, it's a major red flag that you're issuing AJAX requests from a loop. This is highly inefficient; consider making one call and returning an array from the server.

    0 讨论(0)
提交回复
热议问题