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,,");
      }
      });

私の問題は、データベースから要素を 1 つしか取得していないことです

ここの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 に答える