+ 5
Cat API
Okay, so I did .json() method and used the .file cus thats what it said in the url. I dont know what it wrong here and why the cat is not showing https://code.sololearn.com/WhAgtcxgmaKM/?ref=app
37 Answers
+ 2
data = [{...}]
data[0] = {...}
+ 3
My bad in my haste to assist I missed some other things with your code.
I commented them in the code below.
https://code.sololearn.com/Wza4BQ2ITA98
+ 3
From the API documentation:
"Quickstart - Get a random Kitty
Using the language / framework of your choice:
Load https://api.thecatapi.com/v1/images/search
Get the first Array object of the JSON response
Load itโs .url
Enjoy the kitty ๐บ
Bonus: Add a button to repeat the request for a new kitty!"
(https://docs.thecatapi.com/)
The important part for your last question is "Get the first Array object..." therefore you have to use 0 as index. It's just the way the data is structured in the json. Every API should come with documentation of some kind. When none is available you have to look at the file and figure out the structure yourself.
+ 3
We're talking about the brackets at the start.
// initial array (data)
[
// object is the first element of the array at data[0]
{
// properties of the object
"breeds":[],
"categories":[{"id":7,"name":"ties"}],
"id":"u1",
"url":"https://cdn2.thecatapi.com/images/u1.jpg",// data[0].url
"width":625,
"height":446
}
]
+ 3
data[0] gives you the braces and everything between (breeds to height).
+ 3
Your Mom
data ----> [{ ... }] <-array
array is accessed by index
data[0] ----> { ... } <-object
object is accessed by key
data[0].url ----> "https....jpg"
examine what the api returns and process them appropriately.
fun api๐
https://code.sololearn.com/W4wFF883v1V2/?ref=app
+ 2
I do not know. Is that what you got when you searched it up? If so, it may be best to use a different API.
+ 2
For me it is definitely saying something. It says the error one you press the New Cat button.
+ 2
YIPMONG This thread is not for you to post your own post because you are not answering the question of this post
+ 1
Always make sure to check the console when you're not getting exactly what you want. You're getting an accessed blocked error. You can type the error into Google and you might be able to find what the problem is.
+ 1
Did you check your console?
+ 1
In your js code showNewCat(url) it requires an URL, but in HTML an url is not passed to showNewCat() when the button is clicked. Why?
+ 1
Yes I see that and that url will be used for that call to the function and when the function is done that instance of the function is thrown away. When you press the button you are invoking a new instance of showNewCat function and it need an url also.
+ 1
The quick fix is to pass the url via the onclick attribute
onclick="showNewCat('https://api.thecatapi.com/v1/images/search')"
+ 1
Okay thanks just one thing. the array part. why is the data [0]? data[0]. Why is that number 0 is what im asking. why cant it be 3 or 1
+ 1
The API returns an array with one element and array indexes start with 0. This means that the array's first and only element has an index of 0. Hence data[0]. If I were to use data[3] or data[1], undefined would be returned.
https://www.w3schools.com/js/js_arrays.asp
+ 1
okay if im correct wouldnt that mean [0] is the breeds?
+ 1
hm? how so? Wouldnt 0 mean breeds? I tested arrays on w3schools and it only showed the โ0โ object.
+ 1
is it working for yall?
https://code.sololearn.com/WLP80zlWYn8v/?ref=app
0
so theres some kind of malware in that api website?