我刚刚完成了一个jQuery脚本,以便从我在asp.net的webservice中获取JSON响应。
表单现在将与web服务进行通信,并尝试获取自动填充文本框的相应数据,但我似乎无法正确格式化数据,以获取建议区域的数组。
当我在框中键入文本时,我得到像[]这样的括号,而不是像我应该的那样的客户名称。
这是我的aspx页面......
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Search.aspx.cs" Inherits="Search" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"
type = "text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.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 () {
$("#<%=ClientSearch.ClientID %>").autocomplete({
source: function (request, response) {
$.ajax({
url: '<%=ResolveUrl("~/GetClients.asmx/GetClientNames") %>',
data: "{ 'prefix': '" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data.d, function (item) {
return {
label: item.split('-')[0],
val: item.split('-')[1]
}
}))
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
},
select: function (e, i) {
$("#<%=hfClientID.ClientID %>").val(i.item.val);
},
minLength: 1
});
});
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Gadget" Runat="Server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="PageContent" Runat="Server">
<h1>jQuery Autocomplete Lab</h1>
<asp:TextBox ID="ClientSearch" CssClass="autocomplete" runat="server"></asp:TextBox>
<asp:HiddenField ID="hfClientID" runat="server" />
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick = "Submit" />
</asp:Content>
这是我的网络服务
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Configuration;
using System.Web.Script.Services;
using System.Data.SqlServerCe;
using System.Web.Script.Serialization;
/// <summary>
/// Summary description for Service_CS
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 GetClients : System.Web.Services.WebService
{
public GetClients()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
JavaScriptSerializer js = new JavaScriptSerializer();
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetClientNames(string prefix)
{
List<string> clients = new List<string>();
using (SqlCeConnection conn = new SqlCeConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlCeCommand cmd = new SqlCeCommand())
{
cmd.CommandText = "SELECT [Name], [ID] FROM [Current] WHERE " + "[Name] LIKE @SearchText";
prefix = "'" + prefix + "%'";
cmd.Parameters.AddWithValue("@SearchText", prefix);
cmd.Connection = conn;
conn.Open();
using (SqlCeDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
clients.Add(string.Format("{0}-{1}", sdr["Name"], sdr["ID"]));
}
}
conn.Close();
}
string json = js.Serialize(clients);
return json;
}
}
}
任何建议?
答案 0 :(得分:1)
由于您的客户端没有返回任何数据(您在响应中看到的空括号暗示),这意味着您的服务器端web方法存在问题。
尝试在调试模式下运行webmethod,并确保数据检索正常运行,并且正确创建了json序列化字符串。