javascript - getJSON not working with latest jquery but works with older jQuery -


i updating older website using version of jquery 2009. have removed old jquery , added line below use latest version of jquery:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 

the code below takes user selected value dropdown , jquery populates dropdown based on users' selection.

the following code works older version of jquery 2009, when switch latest jquery, not work. there no errors. console clear. added alerts, alert 1 , alert 2 work ...alert 3 never shows up.

$(function(){   $("select#area_name").change(function(){     alert('1');     $.ajaxsetup({ cache: false });     alert('2');     $.getjson("/props/res/get-area.php",{area_name: $(this).val()}, function(j){       var options = '';       alert('3');       (var = 0; < j.length; i++) {         options += '<option value="' + j[i].optionvalue + '">' + j[i].optiondisplay + '</option>';       }       $("select#arearesults").html(options);     });   }); }); 

i'm guessing way jquery handles getjson part has changed, i'm not programmer , helping friend, appreciated.

per jquery docs

the jqxhr.success(), jqxhr.error(), , jqxhr.complete() callback methods introduced in jquery 1.5 deprecated of jquery 1.8. prepare code eventual removal, use jqxhr.done(), jqxhr.fail(), , jqxhr.always() instead.

so change code to:

$(function(){   $("select#area_name").change(function(){     alert('1');     $.ajaxsetup({ cache: false });     alert('2');     $.getjson("/props/res/get-area.php",{area_name: $(this).val()}).done(function(j){       var options = '';       alert('3');       (var = 0; < j.length; i++) {         options += '<option value="' + j[i].optionvalue + '">' + j[i].optiondisplay + '</option>';       }       $("select#arearesults").html(options);     });   }); }); 

edit

as barmar noted, passing success callback parameter of $.getjson hasn't been deprecated.

also docs:

important: of jquery 1.4, if json file contains syntax error, request fail silently. avoid frequent hand-editing of json data reason. json data-interchange format syntax rules stricter of javascript's object literal notation. example, strings represented in json, whether properties or values, must enclosed in double-quotes. details on json format, see http://json.org/.

are sure json in correct format? please post sample of json.


Comments