Forum Moderators: open
{
"products": [
{"id": 1, "href": "http://example.com/products/1"},
{"id": 2, "href": "http://example.com/products/2"},
{"id": 3, "href": "http://example.com/products/3"},
{"id": 4, "href": "http://example.com/products/4"},
{"id": 5, "href": "http://example.com/products/5"}
]
}
The end of the url is "fmt=json"
I guess the fist step I need to do is structure the data (product id's and links)in the api then work on the JS to fetch the data. Right?
<script>
var data = '{"resultsList":[{"dealerId":"56729","dealerName":"Beaman Toyota","dealerAddress":"1525 Broadway|Nashville, TN 37203","dealerServiceRating":"0.0","dealerSaleRating":"1.0","dealerPhone":"(888) 929-6154 ","dealerLatitude":"36.154679","dealerLongitude":"-86.791912","dealerLocationId":"16446","dealerSalesReviewsCount":"2","dealerServiceReviewsCount":"0","dealerMake":null,"inventoryId":"100457874","vin":"4T1BF1FK6EU389311","franchiseId":"56729","make":"Toyota","model":"Camry","submodel":"Camry Sedan","submodelId":"200486708","modelLinkCode":"M030554","modelYearId":"200485954","year":"2014","trim":"SE","actualTrim":"N/A","displayTrim":"SE","msrpPrice":"24875","cityMpg":"25","hwyMpg":"35","combinedMpg":"28","engineSize":"4","exteriorColor":"Cosmic Gray Mica|60,64,79","interiorColor":"Black premium cloth|60,60,60","exteriorGenericColor":"Gray","interiorGenericColor":"Black","styleName":"SE 4dr Sedan (2.5L 4cyl 6A)","styleId":"200497904","transmission":"AUTOMATIC","driveTrain":"front wheel drive","options":["Carpet Floor and Trunk Mat Set (CF)","2014.5 Model Year (MY)","50 State Emissions (FE)"],"features":["Cruise Control","Seat Material: Premium Cloth","Side Airbags: Dual Front And Dual Rear","Pre Wired For Phone","Cd Mp3 Playback","Auxiliary Audio Input And Usb With External Media Control","1st Row Seats: Bucket","Usb Connection","Tire Pressure Monitoring","Stability Control","Traction Control","In Dash Cd: Single Cd Player","Child Seat Anchors"],"bodyType":"Sedan","stockNumber":"00ET1424","invoicePrice":"N/A","guaranteedPrice":"24775.0","tmvinventoryPrice":"23202.0","tmvdealerCash":"0","tmvcustomerIncentives":"726","gpexperiationDate":"2014-06-30"}]}';
var json = JSON.parse(data);
var DealerName = (json.resultsList[0].dealerName);
alert(DealerName);
var vin = (json.resultsList[0].vin);
alert(vin);
var submodel = (json.resultsList[0].submodel);
alert(submodel);
var invenId = (json.resultsList[0].inventoryId);
alert(invenId);
var model = (json.resultsList[0].model);
alert(model);
var driveTrain = (json.resultsList[0].driveTrain);
alert(driveTrain);
</script>
I'm trying to figure out how to tell the script to "go to this url and get this "data"
// Replace apiUrl value with actual url
var apiUrl = 'http://example.com/api/inventory/v1/lookup?foo=bar',
xhr = new XMLHttpRequest();
xhr.open('GET', apiUrl, true);
xhr.onreadystatechange = handler;
xhr.send();
function handler() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
// perfect!
var data = JSON.parse(xhr.responseText);
var DealerName = data.resultsList[0].dealerName;
alert(DealerName);
// ... etc., etc.
} else {
// there was a problem with the request,
// for example the response may contain a 404 (Not Found)
// or 500 (Internal Server Error) response code
}
} else {
// still not ready
}
}