本文介绍了的Ajax调用循环 - 访问循环计数器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里卡住了,任何帮助,将AP preciated。

我有一个项目列表框,我想找回在通过AJAX的列表(调用Web服务)上的每一项数据。所检索的数据需要在此基础上排它从叫做被操纵。如果我通过在该行的参数,它的值总是人比行数越大。有没有一种方法来传递它的时候了ajax呼叫发起的价值呢?

  VAR = numRows行list.options.length;
    对于(VAR行= 0;行< numRows行;行++)
    {
      VAR值= list.options [行] .value的;
      VAR xmlHttpObj = CreateXmlHtt prequestObject();
      如果(xmlHttpObj!= NULL)
      {
        xmlHttpObj.open(POST,Async.ashx ARG1 = GetPhysicalPathInfo和放大器; ARG2 =+值,真正的);
        xmlHttpObj.onreadystatechange =函数(行)
        {
          需要知道我们是从什么行// code
        }
      }
      xmlHttpObj.send();
    }
 

解决方案

通过自动执行的功能创建一个闭包:

  VAR = numRows行list.options.length;
对于(VAR行= 0;行< numRows行;行++)
{
  !函数(行){
      VAR值= list.options [行] .value的;
      VAR xmlHttpObj = CreateXmlHtt prequestObject();
      如果(xmlHttpObj!= NULL)
      {
        xmlHttpObj.open(POST,Async.ashx ARG1 = GetPhysicalPathInfo和放大器; ARG2 =+值,真正的);
        xmlHttpObj.onreadystatechange =功能()
        {
          需要知道我们是从什么行// code
        }
      }
      xmlHttpObj.send();
  }(行);
}
 

当然,那就是你发出从环AJAX请求一大红旗。这是非常低效的;考虑做一个电话,从服务器返回一个数组。

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 retrieved data needs to be manipulated based on which row it was called from.If I pass in the row parameter, its value is always one greater than the number of rows.Is there a way to pass in the value it had at the time the ajax call was initiated?

    var NumRows = list.options.length;
    for ( var row = 0; row < NumRows; 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 ( row )
        {
          // code that needs to know what row we were from
        }
      }
      xmlHttpObj.send();
    }
解决方案

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.

这篇关于的Ajax调用循环 - 访问循环计数器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 07:50