The reason you're getting an error with the asynchronous AJAX is that the function immediately returns, which is well before you get a response from the server. So you're calling JSON.parse()
on garbage data, which is causing the error you're seeing.
The solution is to make the parsing happen after you get a response from the server:
if(year !=""){
//Get vehicle json and store into vehicles_json
getJson(
js_base_url,
function(makes_json) {
//Set makes equal to makes dropdown
var makes = $("#make");
//empty dropdown
makes.empty();
var makes_array = [];
//loop through makes_json json
$.each(makes_json, function() {
makes_array.push('<option value="', this.model_make_display, '">', this.model_make_display, '</option>');
});
makes.html(makes_array.join(''));
}
);
}
function getJson(url, callBack) {
$.ajax({
type: 'GET',
url: url,
dataType: 'json',
global: false,
success: function(json_response) {
callBack(json_response);
}
});
}