【问题标题】:$.getJSON() not working with ASP.NET MVC2 in jQuery 1.5$.getJSON() 不适用于 jQuery 1.5 中的 ASP.NET MVC2
【发布时间】:2011-02-03 16:57:19
【问题描述】:

我正在尝试新的 jQuery 1.5,它破坏了我的应用程序中的一些东西。我调用了生成 JSON 的操作,但出现错误并导致脚本停止。根据 Fiddler 和 Firebug,该操作确实返回 JSON 数据。我没有提供 JSON 数据,但是根据 JSONLint 数据是有效的。

请注意,这在 jQuery 1.4.4 中可以正常工作。

我首先注意到的是 URL:http://localhost:3219/News/GetAllNewsArchives?callback=jQuery15033185029088076134_1296751219270&_=1296751219672

脚本:

// Dropdown box for past articles
$("#article-select").ready(function() {
    $.ajaxSetup({ cache: false });
    $.getJSON('/News/GetAllNewsArchives', null, function(json) {
        var items = "<option value=''>(Select)</option>";
        $.each(json, function(i, item) {
            items += "<option value='" + item.Id + "'>" + subject + "</option>";
        });
        $("#article-select").html(items);
    });
});

行动:

    public ActionResult GetAllNewsArchives()
    {
        return Json(newsRepository.GetAllNewsArchives(), JsonRequestBehavior.AllowGet);
    }

对我做错了什么有任何想法吗?

【问题讨论】:

    标签: json asp.net-mvc-2 jquery-1.5


    【解决方案1】:

    好的,我切换到 $.ajax 调用,我遇到了同样的错误:

    // Dropdown box for past articles
    $("#article-select").ready(function() {
        $.ajax({
            cache: false,
            type: "POST",
            dataType: "json",
            url: "/News/GetAllNewsArchives",
            success: function(json) {
                var items = "<option value=''>(Select)</option>";
                $.each(json, function(i, item) {
                    items += "<option value='" + item.Id + "'>" + subject + "</option>";
                });
                $("#article-select").html(items);
            }
        });
    

    但是,我在$.ajax() documentation 上发现了一些东西。

    从 jQuery 1.5 开始,jQuery 可以将 dataType 从它在 Content-Type 标头中接收到的内容转换为您需要的内容。例如,如果您希望将文本响应视为 XML,请使用“text xml”作为 dataType。您还可以发出 JSONP 请求,将其作为文本接收,并由 jQuery 解释为 XML:“jsonp text xml”。类似地,诸如“jsonp xml”之类的速记字符串将首先尝试从 jsonp 转换为 xml,如果失败,则从 jsonp 转换为文本,然后从文本转换为 xml。

    我将我的数据类型从 dataType: "json" 更改为 dataType: "text json" 然后它起作用了。

    现在,我只是不明白为什么会有差异。 json 期望有什么不同?

    【讨论】:

    【解决方案2】:

    在 jquery 1.5 源代码中,这是检测内容类型的代码。

    // Remove auto dataType and get content-type in the process
    while( dataTypes[ 0 ] === "*" ) {
        dataTypes.shift();
        if ( ct === undefined ) {
            ct = jXHR.getResponseHeader( "content-type" );
        }
    }
    
    // Check if we're dealing with a known content-type
    if ( ct ) {
        for ( type in contents ) {
            if ( contents[ type ] && contents[ type ].test( ct ) ) {
                dataTypes.unshift( type );
                break;
            }
        }
    

    如果您将 dataType 设置为“json”,前面的代码会将 ct 保留为未定义。 这就是为什么它不能按预期工作的原因。 这是 jQuery 1.5 中的一个问题,因为它破坏了与以前版本的 jQuery 的兼容性。

    所以你应该将 dataType 设置为“text json”或删除 dataType 以便使用默认值。

    默认值:

        converters: {
    
            // Convert anything to text
            "* text": window.String,
    
            // Text to html (true = no transformation)
            "text html": true,
    
            // Evaluate text as a json expression
            "text json": jQuery.parseJSON,
    
            // Parse text as xml
            "text xml": jQuery.parseXML
        }
    

    【讨论】:

    • 如何在 $.getJSON 方法上做到这一点?或者我如何发送正确的内容类型?
    • 已在最新的 jQuery 版本 1.5.2 中修复
    【解决方案3】:

    出于某种原因,它会将您的请求解释为 JSONP。在 Firebug 中,检查 $.ajaxSettings 的值并确保没有将其 dataType 默认为 jsonp

    您是否尝试过直接使用$.ajax() 来显式设置请求的typedataType 等?

    【讨论】:

    • 那是我的下一步。但我们不确定我们是否不会得到相同的结果。我会尝试并发布结果。
    【解决方案4】:

    响应中的内容类型是什么?不是您要的内容,也不是在 dataType 参数上指定的内容,而是服务器作为响应的内容类型发回的内容(来自 fiddler)

    【讨论】:

    • Content-Type: application/json; charset=utf-8
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    相关资源
    最近更新 更多