【问题标题】:$.getJSON error on return element返回元素上的 $.getJSON 错误
【发布时间】:2009-05-06 18:16:30
【问题描述】:

仍在使用 JQuery 来获取数据并重新填充页面的某些部分。现在我无法让它返回数据,但它确实弹出一个错误,说“数据未定义”。

代码:

var retrieveData2 = function(path, productGroup, productType, itemsPerPage, pageIndex, filters, fnHandleCallback) {
    $.getJSON(path
             , { productGroup: productGroup, productType: productType, itemsPerPage: itemsPerPage, pageIndex: pageIndex, filter: filter }
             , function(data) { fnHandleCallback(data); });
};

function updateNavIndex(pageIndex) {
    var filters = $("form").serialize();
    var productGroup = $("#valProductGroup").attr('title');
    var productType = $("#valProductType").attr('title');
    var itemsPerPage = $("#ItemsPerPage").val();

    retrieveData2("/CatalogAjaxController/UpdateNavigation", productGroup, productType, itemsPerPage, pageIndex, filters, handleMenuData(data));
}

function handleMenuData(data) {
    $("#CatalogPagingMenu").remove();

    // [http://ejohn.org/blog/javascript-micro-templating/][apply data to template]
    }

当调用 updateNavIndex 函数时,我收到“Microsoft JScript 运行时错误:‘数据’未定义”的错误。

我错过了什么?


啊!更接近(现在有 1 票的答案)- 尚未进行服务器调用。它直接进入回调处理程序。 :(


大部分情况下都能正常工作。我的 URL 指向 CatalogAjaxController。它应该指向 CatalogAjax,因为 MVC 知道它是一个控制器。

【问题讨论】:

    标签: jquery asp.net-mvc


    【解决方案1】:

    在您发布的代码中,retrieveData2 有一个名为 filters 的参数,但您对 $.getJSON() 的调用被传递给 filter: filter 而不是 filter: filters:

    var retrieveData2 = function(path, productGroup, productType, itemsPerPage, pageIndex, filters, fnHandleCallback) {
        $.getJSON(path
                 , { productGroup: productGroup, productType: productType, itemsPerPage: itemsPerPage, pageIndex: pageIndex, filter: filter }
                 , function(data) { fnHandleCallback(data); });
    };
    

    这可能是原因吗?

    【讨论】:

      【解决方案2】:

      在这一行:

      retrieveData2("/CatalogAjaxController/UpdateNavigation", productGroup, productType, itemsPerPage, pageIndex, filters, handleMenuData(data));
      

      您传递给 handleMenuData 的数据在 updateNavIndex 函数的范围内是未定义的。实际上我看不到 updateNavIndex 函数是如何被调用的,它应该如何知道数据参数。


      改变这一行:

      retrieveData2("/CatalogAjaxController/UpdateNavigation", productGroup, productType, itemsPerPage, pageIndex, filters, handleMenuData(data));
      

      到:

      retrieveData2("/CatalogAjaxController/UpdateNavigation", productGroup, productType, itemsPerPage, pageIndex, filters, handleMenuData);
      

      retrieveData2 函数获取最后一个参数一个函数,在你的例子中是 handleMenuData。它将使用在retrieveData2 中定义的数据参数调用它。相信你明白了什么:)

      【讨论】:

      【解决方案3】:

      您将handleMenuData(data) 作为函数调用 传递,而不是函数指针。它不是传递函数,而是传递函数调用的结果。

      要解决此问题,您需要将回调放在引号中:'handleMenuData(data)' 或更好:将其包装在一个内联函数 function() { handleMenuData(data); }

      但看起来数据确实是结果的一部分,在这种情况下,您只需从方法中删除参数:

      retrieveData2("/CatalogAjax/UpdateNavigation", productGroup, productType, itemsPerPage, pageIndex, filters, handleMenuData);
      

      【讨论】:

      • 我有,错误消失了。但是,该操作从未被触发。
      【解决方案4】:

      从您的服务器脚本返回的 json 可能无效或完全为空。发布生成 JSON 的服务器端脚本将有助于诊断此问题。

      您在 Firefox 中遇到任何错误吗?您是否尝试过使用 Firebug 查看服务器响应? Firebug 对于调试这种东西是无价的。

      【讨论】:

      • 它永远不会到达 MVC 的 Contoller/Action 方法。它在调用服务器之前出错。
      【解决方案5】:

      代替:

      retrieveData2("/CatalogAjaxController/UpdateNavigation", productGroup, productType, itemsPerPage, pageIndex, filters, handleMenuData);
      

      我需要做的:

      retrieveData2("/CatalogAjax/UpdateNavigation", productGroup, productType, itemsPerPage, pageIndex, filters, handleMenuData);
      

      由于我的路线,我最终将其更改为:

      retrieveData2("/Catalog/Ajax/Update/Navigation", productGroup, productType, itemsPerPage, pageIndex, filters, handleMenuData);
      

      请注意“控制器”一词现在不是路径的一部分。感谢这方面的帮助。没有完全正确的答案,但所有答案都让我有足够的精力来度过难关。特别感谢 Ben Scheirman 今天的一些时间向我展示 FireBug 并使用带有局部视图的 $.ajax!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-01
        • 2016-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-21
        • 1970-01-01
        • 2016-04-04
        相关资源
        最近更新 更多