0
 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 创建一个数组

请帮忙

4

1 回答 1

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 开发人员工具来检查您的请求和响应,并验证数据是否实际被返回。

于 2012-11-02T00:51:07.567 回答