0

我正在开发一个项目,该项目需要在输入一些数据时自动完成文本框,并且将从数据库中获取数据。

为此,我创建了一个网络服务 -

    [WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//[ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[ScriptService]
public class SearchIssues : System.Web.Services.WebService
{
    //[ScriptMethod]      
    [WebMethod]
    public string[] GetCompletionList(string prefixText)
    {
        DataSet ds = null;
        DataTable dt = null;
        OracleConnection conn = null;
        StringBuilder sb = new StringBuilder();
        try
        {
            conn = new OracleConnection(WebConfigurationManager.ConnectionStrings["Conn"].ToString());
            sb.Append("select issueno from cet_sepcet where issueno like '");
            sb.Append(prefixText);
            sb.Append("%'");
            OracleDataAdapter daRes = new OracleDataAdapter(sb.ToString(), conn);
            ds = new DataSet();
            daRes.Fill(ds);
            dt = ds.Tables[0];
        }         
        catch (Exception exc)
        {

        }

        if (conn.State == ConnectionState.Open)
        {
            conn.Close();
        }
        List<string> IssueList = new List<string>();
        for (int i = 0; i < dt.DataSet.Tables[0].Rows.Count; i++)
        {
            IssueList.Add(dt.DataSet.Tables[0].Rows[i][0].ToString());
        }
        return IssueList.ToArray();
    }
}

我写的jquery ajax方法是——

         $(function() {
        $(".tb").autocomplete({
            source: function(request, response) {               
            debugger;
                $.ajax({
                    url: "SearchIssues.asmx/GetCompletionList",
                    data: request.term,
                    dataType: "json",
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    dataFilter: function(data) { return data; },
                    success: function(data) {
                        response($.map(data.d, function(item) {
                            return {
                                value: item                           
                            }
                        }))
                        //alert('Hello');
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                     debugger;
                        alert(errorThrown);
                    }
                });
            },
            minLength: 1
        });
    });

网络服务运行良好。但是当我尝试从 .aspx 页面调用 web 服务时,问题就来了。它引发内部服务器错误。

我不确定我哪里出错了。请帮忙。

提前致谢。阿基勒

4

3 回答 3

2

我建议您使用 firebug这种方式检查您的 Post 请求和响应是否可以轻松调试您的应用程序。

这个在你的代码中ajax

  data: request.term,

其实应该

  data:  "{'prefixText':'" + request.term+ "'}",

您的服务期望prefixText字符串作为参数,我假设request.term是值。

更新

我不知道什么对你有用,这对我有用:

        <script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
        <script src="js/jquery-ui-1.8.custom.min.js" type="text/javascript"></script>
        <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>

        <script type="text/javascript">
            $(document).ready(function() {          
               $("input#autocomplete").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        url: "/Service/WSDataService.asmx/GetStatesWithAbbr",
                        data: "{'name':'" + $(autocomplete).val() + "'}",
                        dataType: "json",
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        dataFilter: function (data) { return data; },
                        success: function (data) {
                            response($.map(data.d, function (item) {
                                return {
                                    label: item.Name,
                                    value: item.Name
                                }
                            }))
                        }
                    });
                },
                minLength: 1
              });
            });
        </script>

……

     <input id="autocomplete" />

服务 :

   [WebMethod]        
    public List<State> GetStatesWithAbbr(string name)
    {
        List<State> sbStates = new List<State>();
        //Add states to the List
       }
于 2012-08-03T06:11:13.080 回答
0

这段代码可能会抛出异常,所以请再验证一次,可能没有检索到表或类似的东西。

   List<string> IssueList = new List<string>();
      for (int i = 0; i < dt.DataSet.Tables[0].Rows.Count; i++)
      {
         IssueList.Add(dt.DataSet.Tables[0].Rows[i][0].ToString());
     }
    return IssueList.ToArray();
于 2012-08-03T06:12:22.150 回答
0

将此行放在您的 ajax 中: data: '{ "prefixText": "' + request.term + '"}',它会像我一样工作。

于 2013-12-09T11:11:51.240 回答