我在使用自动完成(来自数据库)文本框时遇到了麻烦。它在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
最佳答案
尝试使用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()
关于javascript - 如何解决ASP.NET中Ajax调用的授权问题?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34516905/