Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I cant access the JSON results from an $.ajax() request. I'm getting a result from the api once i select a car model, but I can't access the JSON response to get the information I need to propogate a new drop down list.
HTML
<div class="row">
<div class="form-group col">
<label>Make</label>
<select class="custom-select" id="vMake">
<option selected="" disabled="">Choose Make</option>
<?php
require "config.php";
$sql = "SELECT * FROM tbl_cars";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$cars = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($cars as $cars) {
echo "<option id='".$cars['make']."' value='".$cars['make']."'>".$cars['make']."</option>";
}
?>
</select>
</div>
<div class="form-group">
<label>Model</label>
<select class="custom-select col" id="vModel">
</select>
</div>
</div>
JS
$(document).ready(function(){
$('#vMake').on('change', function(){
var make = $(this).val();
var url = "https://vpic.nhtsa.dot.gov/api/vehicles/GetModelsForMake/" + make + "?format=json";
$.ajax({
url: url,
type: "GET",
dataType: "json",
}).done(function(data){
$('#vModel').empty();
$.each(data, function(i, e){
var model = data[i].Model_Name;
$('#vModel').append('<option value="' + model +'">'+ model + '</option>');
});
});
});
})
My console result
{"Count":163,"Message":"Response returned successfully","SearchCriteria":"Make:Ford","Results":[{"Make_ID":460,"Make_Name":"Ford","Model_ID":1778,"Model_Name":"Crown Victoria"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1779,"Model_Name":"Focus"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1780,"Model_Name":"Fusion"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1781,"Model_Name":"Mustang"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1782,"Model_Name":"Taurus"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1796,"Model_Name":"E-150"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1797,"Model_Name":"Edge"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1798,"Model_Name":"Escape"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1799,"Model_Name":"Expedition"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1800,"Model_Name":"Explorer"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1801,"Model_Name":"F-150"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1802,"Model_Name":"Flex"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1803,"Model_Name":"Ranger"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1804,"Model_Name":"Explorer Sport Trac"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1805,"Model_Name":"F-250"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1806,"Model_Name":"F-350"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1807,"Model_Name":"F-450"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1808,"Model_Name":"F-550"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1809,"Model_Name":"F-650"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1810,"Model_Name":"F-750"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1811,"Model_Name":"Transit Connect"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1817,"Model_Name":"E-250"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1818,"Model_Name":"E-350"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1819,"Model_Name":"E-450"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1820,"Model_Name":"Expedition MAX"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":2345,"Model_Name":"Thunderbird"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":2346,"Model_Name":"GT"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":2347,"Model_Name":"Five Hundred"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":2370,"Model_Name":"Excursion"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":2379,"Model_Name":"Freestyle"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":2384,"Model_Name":"Freestar"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":2737,"Model_Name":"Motorhome Chassis"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":3160,"Model_Name":"Taurus X"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":3267,"Model_Name":"Fiesta"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":3410,"Model_Name":"Commercial Chassis"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":3462,"Model_Name":"C-max"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":3608,"Model_Name":"Transit"},{"Make_ID":1237,"Make_Name":"Affordable Aluminum","Model_ID":4563,"Model_Name":"Affordable Aluminum"},{"Make_ID":1298,"Make_Name":"Eagle Ford Tanks & Trailers LLC","Model_ID":4645,"Model_Name":"Eagle Ford Tanks & Trailers LLC "},{"Make_ID":460,"Make_Name":"Ford","Model_ID":5181,"Model_Name":"Escort"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":5182,"Model_Name":"ZX2"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":5264,"Model_Name":"Windstar"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":5342,"Model_Name":"E-550"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":5346,"Model_Name":"B-750"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":5361,"Model_Name":"F-800"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":5868,"Model_Name":"Explorer Sport"},{"Make_ID":2431,"Make_Name":"Milford Welding & Manufacturing","Model_ID":6243,"Model_Name":"Milford Welding & Manufacturing Inc."},{"Make_ID":460,"Make_Name":"Ford","Model_ID":6703,"Model_Name":"Aspire"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":6730,"Model_Name":"Probe"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":6731,"Model_Name":"Contour"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":6825,"Model_Name":"Bronco"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":6857,"Model_Name":"Aerostar"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":7331,"Model_Name":"F-150 Heritage"},{"Make_ID":3609,"Make_Name":"Waterford Tank and Fabrication","Model_ID":7985,"Model_Name":"Waterford Tank and Fabrication, LTD"},{"Make_ID":4265,"Make_Name":"Milford Pipe & Supply","Model_ID":9047,"Model_Name":"Milford Pipe & Supply"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9213,"Model_Name":"L8501"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9214,"Model_Name":"LT8501"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9215,"Model_Name":"L9501"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9216,"Model_Name":"LT9501"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9217,"Model_Name":"L8511"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9218,"Model_Name":"LT8511"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9219,"Model_Name":"L9511"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9220,"Model_Name":"LT9511"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9221,"Model_Name":"L8513"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9222,"Model_Name":"LT8513"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9223,"Model_Name":"L9513"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9224,"Model_Name":"LT9513"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9225,"Model_Name":"L9522"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9226,"Model_Name":"LT9522"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9227,"Model_Name":"A8513"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9228,"Model_Name":"AT8513"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9229,"Model_Name":"A9513"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9230,"Model_Name":"AT9513"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9231,"Model_Name":"A9522"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9232,"Model_Name":"AT9522"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9268,"Model_Name":"B800"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9270,"Model_Name":"F-Super Duty"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9272,"Model_Name":"F-700"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9282,"Model_Name":"P700"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9283,"Model_Name":"P800"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9284,"Model_Name":"FT900"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9285,"Model_Name":"L8000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9286,"Model_Name":"L9000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9287,"Model_Name":"LL9000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9288,"Model_Name":"LLA9000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9289,"Model_Name":"LLS9000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9290,"Model_Name":"LS8000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9291,"Model_Name":"LS9000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9292,"Model_Name":"LT8000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9293,"Model_Name":"LT9000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9294,"Model_Name":"LTS8000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9295,"Model_Name":"LTS9000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9296,"Model_Name":"LTL9000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9297,"Model_Name":"LTLA9000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9298,"Model_Name":"LTLS9000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9299,"Model_Name":"LA8000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9300,"Model_Name":"LA9000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9301,"Model_Name":"LTA9000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9302,"Model_Name":"LN7000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9303,"Model_Name":"LN8000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9304,"Model_Name":"LN9000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9305,"Model_Name":"LNT8000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9306,"Model_Name":"LNT9000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9307,"Model_Name":"CF8000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9308,"Model_Name":"CFT8000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9312,"Model_Name":"CF7000"},{"Make_ID":4533,"Make_Name":"Fords Trailer Sales","Model_ID":9637,"Model_Name":"Fords Trailer Sales"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":10378,"Model_Name":"Festiva"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":11942,"Model_Name":"Low Cab Forward"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":13958,"Model_Name":"Tempo"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":13977,"Model_Name":"B600"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":13978,"Model_Name":"B700"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":13979,"Model_Name":"F-600"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":13980,"Model_Name":"Recreational Vehicle"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":13981,"Model_Name":"F-590"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":13983,"Model_Name":"P600"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":14148,"Model_Name":"CL9000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":14149,"Model_Name":"CLT9000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":14150,"Model_Name":"Bronco II"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":14152,"Model_Name":"FT800"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":14153,"Model_Name":"CT8000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":14154,"Model_Name":"C800"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":14155,"Model_Name":"C8000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":14196,"Model_Name":"Laser"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":14199,"Model_Name":"LTD"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":14614,"Model_Name":"Fairmont"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":14618,"Model_Name":"Granada"},{"Make_ID":5697,"Make_Name":"ASHFORD MFG ","Model_ID":14916,"Model_Name":"Travel Park "},{"Make_ID":460,"Make_Name":"Ford","Model_ID":14930,"Model_Name":"'34 "},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15160,"Model_Name":"CF6000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15194,"Model_Name":"B7000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15195,"Model_Name":"C7000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15197,"Model_Name":"F7000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15198,"Model_Name":"F8000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15203,"Model_Name":"FT8000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15204,"Model_Name":"Courier"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15205,"Model_Name":"B6000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15206,"Model_Name":"C600"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15208,"Model_Name":"C700"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15209,"Model_Name":"F6000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15223,"Model_Name":"LN600"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15225,"Model_Name":"LN700"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15251,"Model_Name":"L800"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15252,"Model_Name":"LT800"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15254,"Model_Name":"LN800"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15255,"Model_Name":"LNT800"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15273,"Model_Name":"E-100"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15280,"Model_Name":"F-100"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15291,"Model_Name":"CT800"},{"Make_ID":6163,"Make_Name":"Swinford Mfg","Model_ID":16298,"Model_Name":"Swinford Mfg"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":17774,"Model_Name":"Ecosport"},{"Make_ID":6579,"Make_Name":"CRANFORD RADIATOR INC.","Model_ID":17910,"Model_Name":"CRANFORD RADIATOR INC."},{"Make_ID":6674,"Make_Name":"BRADFORD BUILT","Model_ID":18064,"Model_Name":"BRADFORD BUILT"},{"Make_ID":6792,"Make_Name":"Stanford Customs","Model_ID":18235,"Model_Name":"Malibu Sedan"},{"Make_ID":6792,"Make_Name":"Stanford Customs","Model_ID":18236,"Model_Name":"Cordova Sedan"},{"Make_ID":6792,"Make_Name":"Stanford Customs","Model_ID":18237,"Model_Name":"Classic Sedan"},{"Make_ID":8578,"Make_Name":"Bradford #1","Model_ID":22551,"Model_Name":"Bradford #1"},{"Make_ID":8948,"Make_Name":"Stafford's Trailers","Model_ID":23075,"Model_Name":"Stafford's Trailers"},{"Make_ID":9569,"Make_Name":"Medford Steel","Model_ID":23878,"Model_Name":"Medford Steel"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":25742,"Model_Name":"Expedition EL"},{"Make_ID":10240,"Make_Name":"Affordable Trailers","Model_ID":26910,"Model_Name":"Affordable Trailers"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":27411,"Model_Name":"Bronco Sport"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":27440,"Model_Name":"Mustang Mach-E"}]}
I'm trying to get the model name of the result. But as shown on image below I get undefined in my second dropdown. How can I get the information I need?
First of all, remove this:
var data;
You already have a variable called data in that function, so if this line is doing anything it's overwriting that variable and making the whole thing undefined. (And if it's not doing that, it's still entirely superfluous and will serve to only confuse you.)
Aside from that, from your JSON it looks like what you want to loop over isn't data, but data.Results:
$.each(data.Results, function(i, e){
//...
});