【问题标题】:Autocomplete Jquery function returning Internal Server Error while calling webservice调用 web 服务时自动完成 Jquery 函数返回内部服务器错误
【发布时间】:2012-08-01 03:51:47
【问题描述】:

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

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

    [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 服务时,问题就来了。它会引发内部服务器错误。

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

提前致谢。 阿基尔

【问题讨论】:

  • 这个问题还在等待建议和答案...请配合

标签: c# asp.net web-services jquery autocomplete


【解决方案1】:

我建议您使用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
       }

【讨论】:

  • 这肯定将错误从“内部服务器错误”更改为“未知”...其余部分仍然相同...。我确定 sumthing 需要更改,否则此方法中肯定缺少 sumthing .
  • @akhil:请求是否影响了您的服务?尝试放置一个断点并检查
  • @akhil :您提到的网址正确吗?尝试getfirebug.com 调试您的服务。
  • 服务运行良好......而且网址也很好......我在想我写的函数可能无法读取我从服务返回的数组...... . 可能有一些其他代码来处理这种情况....希望您能快速回复....
  • @akhil : 希望这个链接能帮助stackoverflow.com/questions/2471166/… 和你的更相似。
【解决方案2】:

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

   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();

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2013-06-02
      • 2012-05-05
      • 2014-02-22
      • 2016-04-02
      • 2023-03-27
      • 1970-01-01
      • 2012-01-19
      • 2020-05-10
      • 1970-01-01
      相关资源
      最近更新 更多