【问题标题】:SharePoint Web Part : Can't make a jquery ajax call to ashx handlerSharePoint Web 部件:无法对 ashx 处理程序进行 jquery ajax 调用
【发布时间】:2012-02-18 17:19:43
【问题描述】:

我的 Web 部件中有一个可滚动的 GridView,我必须对每个用户滚动进行 AJAX 调用。我在 Web 部件中使用 Jquery 1.7.1 来调用 c# 处理程序类。

我收到错误 500:内部服务器错误。

这是 ascx 的示例:

<div id="divProducts" style="height:300px;overflow:auto">
    <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" EnableViewState="false">
        <AlternatingRowStyle BackColor="White" />
        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <HeaderStyle CssClass="header" BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
    </asp:GridView>
</div>

<div id="divProgress" style="margin-top: -50px;margin-left:150px;z-index:-999">
    <asp:Image ID="image1" ImageUrl="~/_layouts/images/MyWebPart/loading.gif" width="100" height="100" runat="server" />
</div>

<script type="text/javascript">
    $(document).ready(function () {

        //initially hide the loading gif
        $("#divProgress").hide();

        //Attach function to the scroll event of the div
        $("#divProducts").scroll(function () {

            //User has scrolled to the end of the grid. Load new data..
            $("#divProgress").ajaxStart(function () {
                $(this).show();
            });
            $("#divProgress").ajaxStop(function () {
                $(this).hide();
            });

            BindNewData();

        });

    });

    function BindNewData() {
            $.ajax({
                type: "GET",
                url: "/_layouts/MyWebPart/FetchRecordsHandler.ashx",
                success: function (data) {
                    alert('data ', data);
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    alert(thrownError);
                }  
            });
        }
</script>

我添加了一个 ASHX 文件,该文件将部署在我的 Web 部件项目 (Layouts/MyWebPart/FetchRecordsHandler.ashx) 的 Layouts 文件夹中:

<%@ WebHandler Language="C#" Class="MyWebPart.Code.FetchRecordsHandler" CodeBehind="FetchRecordsHandler.cs" %>

我创建了类 FetchRecordsHandler,它使用正确的命名空间实现了 IHttpHandler:

namespace MyWebPart.Code
{
    class FetchRecordsHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {

            context.Response.Write("From the handler at " + DateTime.Now);

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

此方法在我的 Web 部件中不起作用。任何关于从滚动事件到 Web 部件进行 ajax 调用的解决方案或其他技术的想法?

谢谢

【问题讨论】:

    标签: ajax sharepoint web-parts ashx


    【解决方案1】:

    您需要将 .ashx 更改为:

    <%@ WebHandler Language="C#" Class="MyWebPart.Code.FetchRecordsHandler, {my assembly}, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxx" %>
    

    其中 {my assembly} 是已编译的 .dll(不含 .dll)的名称,可能 MyWebPart 和 xxxxxxxxxxxxxxxx 是您的程序集的公钥标记。找到它的一种方法是从您的 Web 部件中查看已部署的 .ascx,第一行左右应该包含类似的内容。

    我想您收到的 500 错误与 SharePoint 无法为您的 .ashx 加载程序集有关。您可以在事件查看器中查看更多详细信息。

    【讨论】:

    • 因为我没有太多时间来完成这项任务,所以我选择了另一种可行的解决方案(使用带有 codeBehind 的 aspx)。第一次有时间,我正在测试您的解决方案。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 2012-01-08
    • 1970-01-01
    • 2011-09-10
    • 2013-06-02
    • 1970-01-01
    相关资源
    最近更新 更多