Ajax使用单个请求调用绑定多个HTML控件

时间:2018-04-27 11:41:09

标签: jquery asp.net ajax web-services

我正在尝试使用Ajax调用绑定多个HTML选择控件,但是我的所有工作后我的进程都失败了。这有什么不对?

这是返回五个数据表的Web服务:

function get_first_line(element) {
  var original = element.text();
  var text = original.trim();
  // if you care about IE change it to normal function
  element.html(text.split('').map((c) => '<span>' + c + '</span>'));
  var chars = element.find('span');
  var offset = chars.eq(0).offset();
  var first = original;
  for (var i = 1; i < chars.length; i++) {
    if (chars.eq(i).offset().top > offset.top) {
      first = text.slice(0, i);
      break;
    }
  }
  element.text(original);
  return first;
}

这是jQuery Ajax调用:

[WebMethod]
public List<List<ListItem>> DealerLoad(string otype, string target, string para)
{
    ds = AL.ExecuteDataSet("sp_dlr_NewDealer_query", CommandType.StoredProcedure, new[]
    {
        new MySqlParameter("OType", otype),
        new MySqlParameter("Target", target),
        new MySqlParameter("id", 0)
    });
    List<ListItem> list1 = new List<ListItem>();
    List<ListItem> list2 = new List<ListItem>();
    List<ListItem> list3 = new List<ListItem>();
    List<ListItem> list4 = new List<ListItem>();
    List<ListItem> list5 = new List<ListItem>();

    List<ListItem> list = new List<ListItem>();
    for (int i = 0; i < ds.Tables.Count; i++) {
        foreach (DataRow dr in ds.Tables[i].Rows)
        {

            list.Add(new ListItem
            {
                Value = dr[0].ToString(),
                Text = dr[1].ToString()
            });

            if (i == 0)
            {
                list1 = list;
            }
            else if (i == 1)
            {
                list2 = list;
            }
            else if (i == 2)
            {
                list3 = list;
            }
            else if (i == 3)
            {
                list4 = list;
            }
            else if (i == 4)
            {
                list5 = list;
            }
        }
    }
    return new List<List<ListItem>>{list1, list2, list3, list4, list5};
}

绑定控制方法

function BindControl(xurl,xdata,xcontrol){

var url = '../WebDeal.asmx/DealerLoad2';
var data = {};
var control = $('#ddl_country').attr('id') + ',' +
                  $('#ddl_countryallow').attr('id') + ',' +
                  $('#ddl_center').attr('id') + ',' +
                  $('#ddl_Superior').attr('id') + ',' +
                  $('#ddl_SuperExe').attr('id');
data.otype = 'select';
data.target = 'GetLoadData';
data.para = '';
BindControl(url, JSON.stringify(data), control);

};

成功通话

$.ajax({
    type: "POST",
    data: xdata,
    url: xurl,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (result) {
        onSuccess1(result, xcontrol);
    },
    error: function (error) {
        alert(error.responseText);
    }
});

1 个答案:

答案 0 :(得分:1)

[WebMethod]
public List<List<ListItem>> DealerLoad(string otype, string target, string para)
{
    ds = AL.ExecuteDataSet("sp_dlr_NewDealer_query", CommandType.StoredProcedure, new[]
    {
        new MySqlParameter("OType", otype),
        new MySqlParameter("Target", target),
        new MySqlParameter("id", 0)
    });
    List<ListItem> list1 = new List<ListItem>();
    List<ListItem> list2 = new List<ListItem>();
    List<ListItem> list3 = new List<ListItem>();
    List<ListItem> list4 = new List<ListItem>();
    List<ListItem> list5 = new List<ListItem>();

    List<ListItem> list = new List<ListItem>();
    for (int i = 0; i < ds.Tables.Count; i++) {
        foreach (DataRow dr in ds.Tables[i].Rows)
        {

            list.Add(new ListItem
            {
                Value = dr[0].ToString(),
                Text = dr[1].ToString()
            });

            if (i == 0)
            {
                list1 = list;
            }
            else if (i == 1)
            {
                list2 = list;
            }
            else if (i == 2)
            {
                list3 = list;
            }
            else if (i == 3)
            {
                list4 = list;
            }
            else if (i == 4)
            {
                list5 = list;
            }
        }
    }



    // *Please try to bind the dropdown here rather than clientside*

    return new List<List<ListItem>>{list1, list2, list3, list4, list5};
}