【问题标题】:AutoCompleteExtender Returns HTML Instead of JSONAutoCompleteExtender 返回 HTML 而不是 JSON
【发布时间】:2017-11-01 12:56:44
【问题描述】:

更新 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 开发者工具中查看“网络”选项卡时,我可以看到对 GetGrowers 的调用,并且在请求标头中,它要求application/json 格式,但在响应标头中,它返回text/html。所以下拉列表中的所有这些字符实际上是 html 中的整个页面。

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

更新:

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

原文:

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

在评估代码中的第 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 框架版本 4.7.02046

在我的 Site.Master 页面中我声明

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

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

<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>

在运行 AutoCompleteExtender 的页面上:

<%@ 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>

我没有在 SO 上找到具有相同错误的 AutoCompleteExtender 问题。我在这里想念什么?错误发生在我的 ServiceMethod 可以返回之前,我知道很多。

【问题讨论】:

  • 很难找到错误的位置。您能否将StaticResources NuGet package 添加到您的项目中,这样脚本就不会被缩小?
  • 我刚刚尝试安装 v17.1.1 和以前的版本,两次 VS 都冻结(不响应)。我在安装任何其他 NuGet 包时没有遇到问题。
  • 对原帖进行了更新。
  • @MikhailTymchukDX 还有另一种安装 StaticResources 的方法吗?
  • 您可以尝试将您的项目添加到the AJAX Control Toolkit solution并在本地调试。

标签: c# asp.net ajaxcontroltoolkit autocompleteextender


【解决方案1】:

ASP.NET 路由好像有问题: https://stackoverflow.com/a/8147357/644496

很遗憾,AJAX Control Toolkit 无法检测路由是否启用。

解决此问题的最简单方法是将带有.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">

【讨论】:

  • 我试过ServicePath="ReceivingStation.aspx",它给出了 401 错误。 ServicePath="ReceivingStation" 的工作方式与不添加 ServicePath 的情况相同(它仍然以 text/html 响应,并且下拉列表是 html 中的整个页面)。
  • 我感觉我的问题与我的 Ajax Control Toolkit 安装或一些 Visual Studio/Build/Web Config 设置有关。我只是在解决另一个问题,Modal Popup Extender 可以在本地工作,但在发布到我的 IIS 服务器时不能。在我的开发人员工具中,我看到 ModalPopup.js 和许多其他 ACT 脚本得到 404。我查看了我的“已发布”项目文件夹,但没有包含所有脚本的 AjaxControlToolkit 文件夹!它只是在发布过程中被遗漏了!我手动添加了它,现在模式弹出窗口可以工作了!
  • 这真的很奇怪。我怀疑您的部署有问题。您如何部署您的网站(XCOPY、Web 发布等)?
  • 我使用“构建”->“发布”->“配置文件”:自定义,“连接”:文件系统,“设置”:发布(未选择“文件发布选项”)。我将它创建的文件夹复制到我的 IIS 网站位置并配置其端口。当我从 VS 运行站点时,AutoCompleteExtender 甚至都不起作用,就像 Modal Popup 一样。
  • 你能分享你的项目吗?看起来部署是正确的。
【解决方案2】:

有效的解决方案是将方法添加到 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 页面的 AutoCompleteExtender 中声明服务路径:

ServicePath="AmericanAppleServices.asmx"

我仍然不知道为什么其他方式不起作用,但现在可以了。 @MikhailTymchukDX 感谢您的帮助!

【讨论】:

  • 是的,这是已知的解决方法,但我以为你知道。不要忘记接受你自己的答案!
  • 我之前实际上已经尝试过,但是我在声明类和 web 方法时一定有一些错误,因为我是从网上某处遵循一个一般示例。
猜你喜欢
  • 2022-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-27
  • 2017-03-28
  • 2019-03-31
相关资源
最近更新 更多