Jquery单独列出项目AJAX

时间:2012-09-27 09:19:31

标签: asp.net ajax jquery asp.net-ajax

 public class searchResult
 {
public int id;
public string name;
}
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
[System.Web.Script.Services.GenerateScriptType(typeof(searchResult))]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment                    the following line.
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{

[WebMethod]
public searchResult[] Search(int txtSearch)
{
//Semuler to slow internet connection
System.Threading.Thread.Sleep(2000);

//Declare collection of searchResult
List<searchResult> resultList = new List<searchResult>();
DataSet ds = Classes.getEmployeeDetailSet("SELECT [Employee ID],[First Name] FROM     [Employee$] WHERE [Employee ID] like '" + txtSearch + "%'");
DataTable dt = new DataTable();
dt = ds.Tables[0];
if (dt.Rows.Count > 0)
{
searchResult result = new searchResult();
result.id = int.Parse(dt.Rows[0]["Employee ID"].ToString());
result.name = dt.Rows[0]["First Name"].ToString();
resultList.Add(result);
}
return resultList.ToArray();
}

我创建了一个像

这样的列表
      List<searchResult> resultList = new List<searchResult>(); this
      and i have return that list
      to call this i have use ajax code like this

         $.ajax({ type: "POST",
      url: "WebService1.asmx/Search", //function that in web service
      data: "{txtSearch:" + $("#txtSearch").val() + "}", // passing value of txtSearch input
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(response) {
      var result = response.d;
      var value = '';
      $.each(result, function(index, res) {
      value = value + res.id + res.name;
      });

      alert(value);
      alert("Record was updated successfully,,");
      },
      error: function(msg) {
      alert("Error while calling web service,,");
      }
      });

我的问题是我只从数据库中获取一个元素

我想我无法将ajax响应中的列表分开

      $.each(result, function(index, res) {
      value = value + res.id + res.name;
      });

我想创建一个使用此id + name

的数组

请帮助

1 个答案:

答案 0 :(得分:0)

正如Robert Slaney所说,你只是从数据库中返回第一个结果。

在服务器端尝试这样的事情。

public class SearchResult
{
    public int ID;
    public string Name;
}

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
[ScriptService]
public class WebService1 : System.Web.Services.WebService
{

    [WebMethod, ScriptMethod]
    public SearchResult[] Search(int employeeID)
    {
        List<SearchResult> resultList = new List<searchResult>();
        DataSet ds = Classes.getEmployeeDetailSet("SELECT [Employee ID],[First Name] FROM [Employee$] WHERE [Employee ID] like '" + employeeID + "%'");
        DataTable dt = ds.Tables[0];

        foreach(DataRow row in dt.Rows)
        {
            resultList.Add(new SearchResult{ ID = int.Parse(row["Employee ID"].ToString()), Name = row["First Name"].ToString()});
        }
        return resultList.ToArray();
    }
}

您还可以使用Chrome Developer Tools检查您的请求和响应,并验证数据是否实际被返回。