javascriptjqueryajaxgetjsonjquery-1.5

Changed from JQuery 1.4 to 1.5.1 and getJson stopped working


Today on a website I was working on I changed the version of jQuery from 1.4 to 1.5.1, however this caused a function which relies on the getJson function to stop working, I have looked at the API and as the request is a getRequest I assumed it was backwards compatible.

Here is the code:

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");
            }
        }
    });
}

Has anyone else had an issue like this?

Thanks, Alex.


Solution

  • Try Using $.ajax() instead and assign dataType: "text json"

    As of jQuery 1.5, jQuery can convert a dataType from what it received in the Content-Type header to what you require. For example, if you want a text response to be treated as XML, use "text xml" for the dataType. You can also make a JSONP request, have it received as text, and interpreted by jQuery as XML: "jsonp text xml." Similarly, a shorthand string such as "jsonp xml" will first attempt to convert from jsonp to xml, and, failing that, convert from jsonp to text, and then from text to xml.