C# AutoCompleteXtender返回HTML而不是JSON

C# AutoCompleteXtender返回HTML而不是JSON,c#,asp.net,ajaxcontroltoolkit,autocompleteextender,C#,Asp.net,Ajaxcontroltoolkit,Autocompleteextender,更新3: GetGrowers调用的代码隐藏: using AjaxControlToolkit; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Script.Services; using System.Web.Services; using System.Web.UI; using System.Web.UI.WebControls;

更新3:

GetGrowers调用的代码隐藏:

using AjaxControlToolkit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.ServiceModel.Web;

namespace AmericanApple
{
public partial class ReceivingStation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        bcDataTextBox.Focus();

    }


    [ScriptMethod]
    [WebMethod]
    public static string[] GetGrowers(string prefixText, int count)
    {
        string[] growers = { "American Apple", "Lewis", "Grice" };
        return growers;
    }
}
}
更新2:

我想我已经发现了问题所在。在Chrome developer tools中查看“网络”选项卡时,我可以看到对GetGrowers的调用,在请求头中,它要求
application/json
格式,但在响应头中,它返回
text/html
。因此,下拉列表中的所有字符实际上是html中的整个页面

在我的工作示例中,Auto-CE调用的响应头是
application/json
。因此,无论出于何种原因,我的项目对此的响应格式不正确。我已经检查了web.config文件,没有发现我的工作示例和我正在处理的项目之间有任何差异

更新:

Chrome中正在发生的事情的图像:

原件:

我无法使AutoCompleteXtender正常工作。在Chrome和FireFox中,我的自动完成结果是一堆(随机?)字符。仅在Internet Explorer中,当我在文本框中单击时,页面冻结,我的Visual Studio输出如下所示:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class AmericanAppleServices : System.Web.Services.WebService
{
在评估代码第39行第3列引发异常
0x800a1391-JavaScript运行时错误:“s”未定义
在eval代码第37行第3列引发异常
0x800a1391-JavaScript运行时错误:“r”未定义
在eval代码第31行第3列引发异常
0x800a1391-JavaScript运行时错误:“e”未定义

…而且一直这样下去

我安装了最新的AjaxControlToolkit:17.1.1。 我使用的是VS Pro 2015版本14.0.25420.01更新3。 .NET Framework版本4.7.02046

在我的站点中。我声明的母版页

在顶部和正文中的“我的脚本管理器”:

<asp:ScriptManager runat="server" EnablePageMethods="true" EnablePartialRendering="true">
        <Scripts>
            <%--To learn more about bundling scripts in ScriptManager see http://go.microsoft.com/fwlink/?LinkID=301884 --%>
            <%--Framework Scripts--%>
            <asp:ScriptReference Name="MsAjaxBundle" />
            <asp:ScriptReference Name="jquery" />
            <asp:ScriptReference Name="bootstrap" />
            <asp:ScriptReference Name="respond" />
            <asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />
            <asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />
            <asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />
            <asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />
            <asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />
            <asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />
            <asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />
            <asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
            <asp:ScriptReference Name="WebFormsBundle" />
            <%--Site Scripts--%>
        </Scripts>
    </asp:ScriptManager>

在运行AutoCompleteXtender的页面上:

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<cc1:AutoCompleteExtender ID="AutoCompleteGrower" runat="server" TargetControlID="txtGrower"
    MinimumPrefixLength="0" CompletionInterval="100" EnableCaching="true" UseContextKey="false"
    ServiceMethod="GetGrowers" CompletionListCssClass="autocomplete_List" CompletionListItemCssClass="autocomplete_ListItem"
    CompletionListHighlightedItemCssClass="autocomplete_HighlightedListItem" FirstRowSelected="true">
</cc1:AutoCompleteExtender>

我的文本框:

<asp:TableCell><asp:TextBox ID="txtGrower" runat="server" CssClass="form-control" AutoCompleteType="None"></asp:TextBox></asp:TableCell>


我没有发现关于自动完成TeXtender的问题,因此出现了相同的错误。我错过了什么?错误发生在我的ServiceMethod返回之前,我知道很多。

似乎ASP.NET路由有问题:

不幸的是,AJAX控件工具包无法检测是否启用了路由

解决此问题的最简单方法是将带有
.aspx
扩展名的页面指定为
ServicePath

<cc1:AutoCompleteExtender
        ID="AutoCompleteGrower"
        runat="server"
        TargetControlID="txtGrower"
        MinimumPrefixLength="1"
        CompletionInterval="100"
        EnableCaching="true"
        UseContextKey="false"
        ServicePath="ReceivingStation.aspx"
        ServiceMethod="GetGrowers"
        FirstRowSelected="true">

ASP.NET路由似乎有问题:

不幸的是,AJAX控件工具包无法检测是否启用了路由

解决此问题的最简单方法是将带有
.aspx
扩展名的页面指定为
ServicePath

<cc1:AutoCompleteExtender
        ID="AutoCompleteGrower"
        runat="server"
        TargetControlID="txtGrower"
        MinimumPrefixLength="1"
        CompletionInterval="100"
        EnableCaching="true"
        UseContextKey="false"
        ServicePath="ReceivingStation.aspx"
        ServiceMethod="GetGrowers"
        FirstRowSelected="true">

有效的解决方案是将该方法添加到web服务文件(asmx.cs)中

因此,类声明如下所示:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class AmericanAppleServices : System.Web.Services.WebService
{
方法声明:

[WebMethod]
public string[] GetGrowers(string prefixText, int count)
{
使用此方法,我还必须在.aspx页面的AutoCompleteXtender中声明服务路径:

ServicePath=“AmericanAppleServices.asmx”


我仍然不知道为什么另一种方法不起作用,但现在就可以了@MikhailTymchukDX感谢您的帮助

有效的解决方案是将该方法添加到web服务文件(asmx.cs)中

因此,类声明如下所示:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class AmericanAppleServices : System.Web.Services.WebService
{
方法声明:

[WebMethod]
public string[] GetGrowers(string prefixText, int count)
{
使用此方法,我还必须在.aspx页面的AutoCompleteXtender中声明服务路径:

ServicePath=“AmericanAppleServices.asmx”


我仍然不知道为什么另一种方法不起作用,但现在就可以了@MikhailTymchukDX感谢您的帮助

很难找到错误的位置。您可以添加到您的项目中,这样脚本就不会缩小吗?我刚刚尝试安装v17.1.1和以前的版本,两次都是VS冻结(没有响应)。我在安装任何其他NuGet软件包时没有遇到问题。对原始帖子进行了更新。@MikhailTymchukDX是否有其他方法安装StaticResources?您可以尝试将项目添加到中并在本地进行调试。很难找到错误的位置。您可以添加到您的项目中,这样脚本就不会缩小吗?我刚刚尝试安装v17.1.1和以前的版本,两次都是VS冻结(没有响应)。我在安装任何其他NuGet软件包时没有遇到问题。对原始帖子进行了更新。@MikhailTymchukDX是否有其他方法安装StaticResources?您可以尝试将项目添加到中并在本地进行调试。我尝试了
ServicePath=“ReceivingStation.aspx”
,这会导致401错误。而
ServicePath=“ReceivingStation”
的工作原理与不添加ServicePath的工作原理相同(它仍然以文本/html响应,下拉列表是整个页面的html格式)。我感觉我的问题与我的Ajax控件工具包安装或一些visualstudio/Build/Web配置设置有关。我只是在解决另一个问题,模态弹出扩展器可以在本地工作,但在发布到我的IIS服务器时不能工作。在我的开发工具中,我看到ModalPopup.js和许多其他ACT脚本得到了404。我查看了我的“已发布”项目文件夹,但包含所有脚本的AjaxControlToolkit文件夹不在那里!它只是在出版时被遗漏了!我手动添加了它,现在模式弹出窗口工作了!真奇怪。我怀疑你的部署有问题。如何部署站点(XCOPY、Web发布等)?我使用“构建”->“发布”->“配置文件”:自定义,“连接”:文件系统,“设置”:发布(未选择“文件发布选项”)。我将它创建的文件夹复制到我的IIS网站位置并配置其端口。AutoCompleteXtender甚至不起作用