Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialTadjiev Codes
9,626 PointsAJAX JS issues
Dear Folks or Dear Mr.Steven, Please if someone could help me to understand what am I doing wrong) First, on line 7 the array beerProducts that I have should store returned information as a JavaScript object. I guess JSON.parse() should do that but can't figure it out? Secondly, in my getJSONAsync(URL) function also seems like something is wrong. On the console, it gives an error as it can't read the name. I'm trying to access the data of JSON that's stored in the Postman App. These are the details of the API URL, key: API Url: https://rsd05guo67.execute-api.us-east-2.amazonaws.com/default/beerstore API key: 32isbi7Lug22v8CKogygf5b2EZwpdFhS2OotvWem If you are using βvar request = new XMLHttpRequest();β , you just need to add the following after request.open:
βrequest.setRequestHeader('x-api-key', '32isbi7Lug22v8CKogygf5b2EZwpdFhS2OotvWem');β
I just finished the course of AJAX Basics and still can't realize what I'm doing wrong( Workspace: https://w.trhou.se/ahdiz0iidj This seems to me to be a big jump from what I learned so far( Thanks in advance)))))))
3 Answers
Zimri Leijen
11,835 PointsThere's a couple of issues.
- You start from index 1, not 0, which means the first result is actually the second result.
- You never actually set the value of beerProducts. In the getJSONAsync function you should do beerProducts = jsonObject; (on the line after where you define jsonObject)
in your html you don't set the url, you just name url, but you don't provide the actual URL anywhere in your code.
<button onclick="getJSONAsync('https://rsd05guo67.execute-api.us-east-2.amazonaws.com/default/beerstore')">
you don't pass the currentIndex from Next and PreviousBeer to RenderSelectedBeer. (also, the naming of the functions is inconsistent).
function PreviousBeer() {
// if currentIndex less than 0 or equals to 0 than currentIndex - 1 so our Array starts from 1 instead of 0
currentIndex = (currentIndex <= 0) ? currentIndex : (currentIndex - 1);
// calling the function here to show the result of the Rendering of playback output in other words
RenderSelectedBeer(currentIndex);
}
function Next() {
// If currentIndex greater or equals array's length then -1 else which is ? currentIndex + 1
currentIndex = (currentIndex >= beerProducts.length - 1) ? currentIndex : (currentIndex + 1);
// calling the function here to show the result of the Rendering of playback output in other words
RenderSelectedBeer(currentIndex);
}
function RenderSelectedBeer(currentIndex) {
document.getElementById('idAuthor').innerHTML = beerProducts[currentIndex].name;
// We could have done jsonObject[15].download_url; to access specifically to the fifteenth JSON object inside the array
document.getElementById('beerName').src = beerProducts[currentIndex].image_url;
}
these changes should at least fix the issues.
Tadjiev Codes
9,626 PointsWow Thanks a lot,πππ I thought that
function helper() {
var url = "https://rsd05guo67.execute-api.us-east-2.amazonaws.com/default/beerstore";
getJSONAsync(url);
}
this was providing URL but anyways I did as u said as argument for onclick event. New workspace: https://w.trhou.se/0fbp369z27 The only problem still that it doesn't show the image? Am I still doing sth wrong?
var beerProducts = [];
// Global current Index
var currentIndex = 0;
function PreviousBeer() {
// if currentIndex less than 0 or equals to 0 than currentIndex - 1 so our Array starts from 1 instead of 0
currentIndex = (currentIndex <= 0) ? currentIndex : (currentIndex - 1);
// calling the function here to show the result of the Rendering of playback output in other words
RenderSelectedBeer(currentIndex);
}
function Next() {
// If currentIndex greater or equals array's length then -1 else which is ? currentIndex + 1
currentIndex = (currentIndex >= beerProducts.length - 1) ? currentIndex : (currentIndex + 1);
// calling the function here to show the result of the Rendering of playback output in other words
RenderSelectedBeer(currentIndex);
}
function RenderSelectedBeer(currentIndex) {
document.getElementById('idAuthor').innerHTML = beerProducts[currentIndex].name;
// We could have done jsonObject[15].download_url; to access specifically to the fifteenth JSON object inside the array
document.getElementById('beerName').src = beerProducts[currentIndex].image_url;
}
// Steps in creating AJAX >>>>>>
// 1) Create an XMLHttpRequest Object.
// 2) Create or Define a Callback Function.
// 3) Open a Request (Which is usually GET or POST). request.open("GET", url)
// 4) Send the Request (Which is usually GET or POST). request.send();
// Define your async ajax call function
function getJSONAsync(url) {
// iN THIS THEME ALL THE AJAX AND JSON RELATED KEYWORDS HIGHLIGHTED IN YELLOW
// Or could name the variable as hxr as that's definition of AJAX
var request = new XMLHttpRequest();
//onreadystatechange event and then callback function
request.onreadystatechange = function() {
// check if is finished readystate === 4 and everything is OK
if (request.readyState === 4 && request.status === 200) {
var jsonString = request.responseText;
// handle resposne text here
var jsonObject = JSON.parse(request.responseText);
//Making the Empty array equal to the JSON object which is getting parsed converted to the JavaScript Object
beerProducts = jsonObject;
document.getElementById('idAuthor').innerHTML = jsonObject[currentIndex].name;
// We could have done jsonObject[15].download_url; to access specifically to the fifteenth JSON object inside the array
document.getElementById('beerName').src = jsonObject[currentIndex].image_url;
//var a = 0; for breakpoints if to test on dev tools
}
}
// open a connection using URL
request.open("GET", url)
// Send the GET request
request.setRequestHeader('x-api-key', '32isbi7Lug22v8CKogygf5b2EZwpdFhS2OotvWem');
request.send();
}
Thanks
Zimri Leijen
11,835 PointsI didn't realize there was an image.
Tbh I didn't read over all of the code, just the parts that were throwing errors (the console is very useful in showing you were the errors are).
You could make a helper function for providing the URL, or simply have it be defined in the function itself, but if you pass the url as an argument when you call the function from the html, it will overwrite the value you have set. That's why I just put it in the html.
So, some of the links the API provides are broken, so some images won't work, but if you just change <div id="beerName"><div> to <img id="beerName" /> it will at least work for the links that aren't broken.
Tadjiev Codes
9,626 PointsOh Yeah You're right Mr.Zimri) Such stupid small mistakes I make( Thanks a Lot