【问题标题】:jqGrid not loading data when site published to server网站发布到服务器时jqGrid不加载数据
【发布时间】:2011-10-26 11:12:07
【问题描述】:

我的 jqGrid 有问题。我在这里看到过其他类似问题的帖子,除了我的特别之处在于数据在我的开发机器中正确加载但是当我在生产服务器上发布站点时,jqGrid 不会加载数据,我得到的只是一个空的网格。除了 jqGrid 之外,服务器中对数据的所有其他 ajax 请求都可以正常工作。我的项目在 MVC3 中,我在 win2008 IIS7 中托管该站点。这是我当前的代码:

查看:

<script type="text/javascript">
$(document).ready(function () {
    $("#grid").jqGrid({
        url: '@Url.Action("LoadAction", "Controller", new { area = "Area" })',
        editurl: '@Url.Action("SaveAction", "Controller", new { area = "Area" })',
        datatype: 'json',
        mtype: 'POST',
        colNames: [
            '@Html.LabelFor(v => new foo.bar.MyClass().Property1)',
            '@Html.LabelFor(v => new foo.bar.MyClass().Property2)',
            '@Html.LabelFor(v => new foo.bar.MyClass().Property3)',
            '@Html.LabelFor(v => new foo.bar.MyClass().Property4)'
        ], 
        colModel: [
            { name: 'Property1', index: 'Property1', editable: true },
            { name: 'Property2', index: 'Property2', editable: true, edittype: "select", editoptions: { value: "Option1:Option1;Option2:Option2"} },
            { name: 'Property3', index: 'Property3', editable: true },
            { name: 'Property4', index: 'Property4', editable: true }
        ],
        pager: $('#gridPager'),
        rowNum: 20,
        rowList: [10, 20, 50, 100],
        sortname: 'Property1',
        sortorder: "asc",
        viewrecords: true,
        imgpath: '',
        shrinkToFit: true,
        width: 940,
        height: 300,
        gridview: true
    });
});
</script>
<div>
    <table id="grid" class="scroll" cellpadding="0" cellspacing="0"></table>
    <div id="gridPager" class="scroll" style="text-align:center;"></div>
</div>

控制器:

[Authorize]
public JsonResult LoadAction(string sidx, string sord, int page, int rows)
{
    List<foo.bar.MyClass> list = foo.bar.MyClassController.getAll();

    int pageIndex = Convert.ToInt32(page) - 1;
    int pageSize = rows;
    int totalRecords = list.Count();
    int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);

    var jsonData = new
    {
        total = totalPages,
        page,
        records = totalRecords,
        rows = (
            from myclass in list
            select new
            {
                id = myclass.Id,
                cell = new string[] { 
                    myclass.Property1, 
                    myclass.Property2, 
                    myclass.Property3, 
                    myclass.Property4
                }
            }).ToArray()
    };
    return Json(jsonData);
}

有人知道可能是什么问题吗?

谢谢。

【问题讨论】:

    标签: json asp.net-mvc-3 jqgrid


    【解决方案1】:

    1) 我建议您在 jqGrid 定义中包含 loadError 事件处理程序。在这种方式下,您将看到来自服务器的错误响应。请参阅the answer 的更新部分。可以从here加载对应的demo工程。

    2) 您应该验证发布站点的虚拟目录的配置。您使用 [Authorize] 属性。所以应该明确禁用“匿名身份验证”并启用“Windows 身份验证”、“表单身份验证”或您计划使用的其他身份验证。

    【讨论】:

    • 感谢您的 loadError 建议,我将其包含在内。我在我网站中的几乎每个操作上都使用 [Authorize],并且它工作正常,所以我怀疑这就是问题...
    • 无论如何,您都应该确切地知道您遇到了哪个错误。在another answer 中,您可以找到loadError 的另一种变体。您可以只包含alert(jqXHR.responseText)FiddlerFirebug 之类的工具也很有帮助。在您看到您从服务器收到的错误之后,您可以朝着正确的方向进行下一步。
    • 我现在可以显示多个网格,但其中一个特别给我一个错误...我收到 500 内部服务器错误,没有更多详细信息。你知道这可能是什么吗?
    • @AJC:这是服务器代码的一般异常。您应该验证 web.config 中哪个值具有 includeExceptionDetailInFaults
    • 你是对的......从服务器中的浏览器进行测试时,您的 loadError 处理程序向我显示了我遇到的错误......谢谢。
    【解决方案2】:

    可能有多种原因,主要是 Web 服务器 IIS 设置 - 我猜它通常是安全设置,所以您最好调试它以找出真正的原因。你能在 firefox 中使用 firebug 并找出服务器响应请求时真正​​的错误信息是什么吗? (Firebug -> 控制台 -> 适当的项目 -> 响应)您可以从那里开始。

    我会尝试的一件事是删除 Authorize 属性并查看它是否有效。

    根据你的调试结果OK,说明你的路由有问题。我想我找到了原因。

     public JsonResult LoadAction(string sidx, string sord, int page, int rows)
    

    但是你的 Url.ActionLink 是

       '@Url.Action("LoadAction", "Controller", new { area = "Area" })',
    

    您没有在此处传递参数,因此它会导致 500 错误。请修复它,您将得到结果。

    【讨论】:

    • 感谢您的回复...我将使用 firefox 进行检查,看看是否能发现错误。我似乎无法理解的是为什么网站上的其他地方,所有其他请求都运行良好。这就是为什么我认为它可能是 jqGrid 的问题或错误。我可以尝试删除 [Authorize],但必须这样做,因为该操作只能由授权用户执行。
    • 如果其他调用正常,可能不是安全问题,您可以忽略它。让我们看看 firebug 是怎么回事。
    • 感谢您对使用 firefox 的建议...从来没有这样使用过。它帮助我发现了一个不同的问题,但与这个问题相关。
    • 抱歉,错误仍然存​​在...我现在收到 500 内部服务器错误,没有更多详细信息。
    • 我想我找到了问题所在。更新了我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-23
    • 1970-01-01
    • 2014-01-21
    相关资源
    最近更新 更多