我在使用自动完成(来自数据库)文本框时遇到了问题。它在ASP.NET(VB)网页中对WebMethod进行ajax调用,并在文本框中键入字母时给出“身份验证错误”。
aspx页面代码如下:
<body>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script language="javascript" type="text/javascript">
$(function () {
$('#<%=txtCompanyName.ClientID%>').autocomplete({
source: function (request, response) {
$.ajax({
url: '<%= ResolveUrl("VendorList.aspx/GetCompanyName")%>',
data: "{ 'pre':'" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data.d, function (item) {
return { value: item }
}))
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.responseText);
}
});
}
});
});
</script>
<div class="ui-widget" style="text-align:left">
<asp:TextBox ID="txtCompanyName" runat="server" Width="350px" CssClass="textboxAuto" Font-Size="12px" />
</div>
</body>
文件代码背后的VB.NET代码如下:
Partial Class VendorList
<System.Web.Services.WebMethod()> _
<System.Web.Script.Services.ScriptMethod(ResponseFormat:=Script.Services.ResponseFormat.Json)> _
Public Shared Function GetCompanyName(ByVal pre As String) As List(Of String)
Dim allCompanyNames As New List(Of String)
Using myEntities As New VendorEntities()
allCompanyNames = (From vendor In myEntities.Vendors
Where vendor.VendorName.StartsWith(pre)
Select vendor.VendorName).ToList()
End Using
Return allCompanyNames
End Function
End Class
我还尝试在包含此页面的目录中创建一个Web.config文件,其代码如下:
<?xml version="1.0"?>
<configuration>
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</configuration>
我也在使用FriendlyURLS。
有关如何解决此身份验证错误的任何想法?感谢
的更新
我现在也尝试过AjaxControlToolkit AutoCompleteExtender,但我只返回页面的HTML标记列表,每个列表项有一个字符。
aspx页面的代码如下:
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods = "true"></asp:ScriptManager>
<asp:TextBox ID="txtVendorNameSearch" runat="server"></asp:TextBox>
<cc1:AutoCompleteExtender
MinimumPrefixLength="2" ServiceMethod="SearchVendorNames"
CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"
TargetControlID="txtVendorNameSearch"
ID="AutoCompleteExtender1" runat="server" FirstRowSelected = "false">
</cc1:AutoCompleteExtender>
</asp:Content>
VB.NET代码隐藏文件的代码如下:
Partial Class Operations_Inventory_AutoCompleteTest
Inherits BasePage
<System.Web.Script.Services.ScriptMethod(), _
System.Web.Services.WebMethod()> _
Public Function SearchVendorNames(ByVal prefixText As String, ByVal count As Integer) As List(Of String)
Dim vendorNames As List(Of String) = New List(Of String)
Using myEntities As New VendorEntities()
Dim vendors = (From vendor In myEntities.InventoryVendors
Where vendor.VendorName.StartsWith(prefixText)
Select vendor.VendorName, vendor.ID).ToList()
For Each item In vendors
vendorNames.Add(AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(item.VendorName, item.ID.ToString))
Next
End Using
Return vendorNames
End Function
End Class
答案 0 :(得分:0)
尝试使用Ajax AutoCompleteExtender
您的WebMethod应如下更改:
你应该返回类似下面的内容
List<string> items = new List<string>();
foreach (var companyName in allCompanyNames)
items.Add(AutoCompleteExtender.CreateAutoCompleteItem(companyName["VendorName"], companyName["ID"].ToString());
return items;
我希望您在LINQ查询中选择公司名称ID。
allCompanyNames = (From vendor In myEntities.Vendors
Where vendor.VendorName.StartsWith(pre)
Select vendor.VendorName, vendor.ID).ToList()
答案 1 :(得分:0)
添加
beforeSend: function (xhr) { xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); },
为我的错误而努力。
$.ajax({
type: 'POST',
beforeSend: function (xhr) { xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); },
contentType: "application/json; charset=utf-8",