【问题标题】:why are not getting data in sdr it from database?为什么不从数据库中获取 sdr 中的数据?
【发布时间】:2014-11-24 09:55:55
【问题描述】:

## 在这段代码中,为什么没有从数据库中获取 sdr 中的数据? ##

下面的代码是业务层类:

public string[] AutoComplete(string Address)
{
   DtAccess objDataAccess = new DtAccess();
   List<string> request = new List<string>();

   SqlParameter[] param = new SqlParameter[2];
   param[0] = new SqlParameter("@Mode", "AutoComplete");
   param[1] = new SqlParameter("@Address", Address);
   using (SqlDataReader sdr = objDataAccess.ExecuteReader(_spName, param))
   {
     while (sdr.Read())
     {
       request.Add(string.Format("{0}-{1}-{2}", sdr["Address"], sdr["Latitute"], sdr["Longitute"]));
      }
   }
   return request.ToArray();
}

下面的代码是example.aspx.cs页面:

 [WebMethod]
    public static string[] GetAutoCompleteData(string Address)
    {
        BussinessLgc objBussinessLogic = new BussinessLgc();
        return objBussinessLogic.AutoComplete(Address);            
    }

[更新]

Ajax 代码如下:

$(document).ready(function () {
    $("#toAddress").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: '<%=ResolveUrl("~/Root.aspx/GetAutoCompleteData") %>',
                data: "{ 'Address': '" + request.term + "'}",
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    if (data.d.length == 0) {
                        $('.ui-autocomplete').hide();
                    }
                    else {
                        $(".pac-container").hide();
                        response($.map(data.d, function (item) {
                            return {
                                label: item.split('-')[0],
                                val: item.split('-')[1],
                                long: item.split('-')[2]
                            }
                        }))
                    }
                },
                error: function (response) {
                    alert(response.responseText);
                },
                failure: function (response) {
                    alert(response.responseText);
                }
            });
        },
        select: function (e, i) {
            $("#<%=DestinationSLat.ClientID %>").val(i.item.val);
            $("#<%=DestinationELng.ClientID %>").val(i.item.long);
        },
        minLength: 2
    });
});

我在jQuery 上面尝试从GetAutoCompleteData WebMethod 获取数据。但是数据不是来自数据库的 srd..

【问题讨论】:

  • 发布您的Ajax Jquery,它运行上述网络服务
  • 您是否尝试过在 SQL Server 控制台中执行相同的操作并尝试捕获是否有异常。

标签: jquery asp.net web-services architecture


【解决方案1】:

尝试更改 aspx/Webmethod 的路径

url: '<%=ResolveUrl("~/Root.aspx/GetAutoCompleteData") %>'

 url: '<%=ResolveUrl("Root.aspx/GetAutoCompleteData") %>' //with out ~/

也将TRY...CATCH 放入您的Business layer 函数中,就像@Arindam Nayak 所说的那样

【讨论】:

  • 检查 Root.Aspx 的路径,并在你的 JQuery 中放一个断点并检查它
  • Root.aspx 的路径是完美的。我使用断点检查,然后 data.d.length 总是 0 .
  • 0 表示您没有根据您输入的值获取任何数据尝试使用相同数据在 SSMS 中运行您的 SP
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-17
  • 1970-01-01
  • 2021-11-26
  • 2013-08-11
  • 1970-01-01
  • 2020-11-26
  • 2015-11-12
相关资源
最近更新 更多