2

今天在我正在开发的一个网站上,我将 jQuery 的版本从 1.4 更改为 1.5.1,但这导致依赖于该getJson函数的函数停止工作,我查看了 API,因为请求是我假设的 getRequest它是向后兼容的。

这是代码:

function EmailAutoComplete(firstName, lastName, target) {
    // Query /AutoComplete/Email?FirstName=&LastName= for an e-mail
    // list and populate the select box target with the results.
    $.getJSON('@Url.Action("AutoComplete", "Email")', {
        FirstName: firstName,
        LastName: lastName
    }, function(matchingEmails) {
        var oldVal = target.val();
        target.empty();
        if (matchingEmails == null || matchingEmails.length == 0) {
            target.append('<option value="">E-mail address not found</option>');
        } else {
            $.each(matchingEmails, function(key, val) {
                var selected = (val == oldVal) ? 'selected="selected"' : '';
                target.append('<option value="' + val + '" ' + selected + '>' + val + '</option>');
            });

            if (matchingEmails.length > 1) {
                target.addClass("multipleEmailsAvailable");
            } else {
                target.removeClass("multipleEmailsAvailable");
            }
        }
    });
}

有没有其他人遇到过这样的问题?

谢谢,亚历克斯。

4

2 回答 2

3

尝试使用$.ajax()并分配dataType: "text json"

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

于 2011-03-14T05:28:04.147 回答
0

我遇到了同样的问题。

原来我的 json 文件无效。

修复我的 json 文件后,getJson 再次发挥了魅力。

于 2011-06-24T23:55:08.937 回答