#javascript #fetchapi How to use JavaScript Fetch API to read a local JSON file and how show the result in console. How to show fetched data in web developer console. JavaScript Shorts: • JavaScript Shorts
Пікірлер: 67
@ashwinajmery6071 Жыл бұрын
Its feel rainy season...thats good
@5095smf Жыл бұрын
I love the rain sounds. Thanks for the tutorial!
@oneminutecoder Жыл бұрын
Thanks
@exoaxse2256 Жыл бұрын
thank you very much finally after a few day resolve thanks bro
@oneminutecoder Жыл бұрын
Thanks
@Lighter7900music Жыл бұрын
Thanks man it was helpful!
@danielsanchezmanez51535 ай бұрын
It works. Amazing. Thank you
@NTFG2 жыл бұрын
Put the sound of typing in your next video
@NTFG2 жыл бұрын
@@itsmaxim01 cuz
@poknari Жыл бұрын
my country here: Indonesia
@dilemma0866 ай бұрын
Thanks!!!
@laneW612 Жыл бұрын
thanks buddy!
@oneminutecoder Жыл бұрын
Glad to help!
@ertekinertunc Жыл бұрын
thanks man, quick and easy solution. I have a question, how can I move the variable inside the function out?
@oneminutecoder Жыл бұрын
Şöyle kullanabiliriz let jsonData; fetch("test.json") .then(response => response.json()) .then(data => { jsonData = data; showInfo(); }); function showInfo() { console.log(jsonData); }
@oneminutecoder Жыл бұрын
Veya böyle fetch("test.json") .then(response => response.json()) .then(data => { showInfo(data); }); function showInfo(data) { console.log(data); }
@oneminutecoder Жыл бұрын
Ya da böyle async function fetchData() { const response = await fetch("test.json"); const data = await response.json(); console.log(data); } fetchData();
@ertekinertunc Жыл бұрын
Çok teşekkür ederim 🙂🙂
@vignesh-l3i2 ай бұрын
then how to print that data inside the document ?? bro plz replay .
@oneminutecoderАй бұрын
How to use response: kzbin.info/www/bejne/bJnTmJ56isulh6s kzbin.info/www/bejne/q6K0ooykrJaUhcUsi=9Rqd9-0ujbueya2r kzbin.info/www/bejne/q6K0ooykrJaUhcUsi=LM8WEjaVLUmEBZzN kzbin.info/www/bejne/qYvKn6mioLN9o6csi=CPcISYEl0kt--D-Q kzbin.info/www/bejne/q3WrqJmmqM6frassi=Iagk81OA3GO7Vg_S
@tirthpatel4624 Жыл бұрын
Similarly, is it possible to update/write data into json file using fetch or any other function..for me Im trying to update using fs but giving error as writeFile not function
@jimmlmao11 ай бұрын
No, unless you write server side code to do that but thats a major security problem
@satishsgm0157 Жыл бұрын
Do video on how to fetch json file from remote api also brother 😊
@oneminutecoder Жыл бұрын
Isn't that already?
@antcannon8 ай бұрын
new to APIs i am looking to find a way for this to show in my terminal. get an ERR_INVALID_URL error
@unknownhacker94167 ай бұрын
Can you please help me I have name and regarding mail I'd in json file when I dynamically call the name and mails to usin Javascript in my html code in that time i am getting only names not emails what is the cause
@gouravnainwaya56692 жыл бұрын
Does this are possible in axios in react native
@oneminutecoder2 жыл бұрын
Yes possible. React Native has a built-in Fetch API. But i am not a developer of React Native.
@carloseduardomenendezm.23898 ай бұрын
Hey i would like to know how did you do to read the local file because i always get an error message telling me something about cross references policy!!!!
@parveenchoudhary82582 жыл бұрын
Why it is working on live server but not on chrome.
@oneminutecoder2 жыл бұрын
Fetch API makes HTTP requests to servers. Request URL should be URL: http... and not should be URL: file:///C:/Users/ if you use liveserver JavaScript sees test.json like this Request URL: 127.0.0.1:5500/test.json
@f4jrie09 Жыл бұрын
@@oneminutecoder
@aryansoni8141 Жыл бұрын
Which theme is this?
@oneminutecoder Жыл бұрын
i don't remember.
@roselynbasan132611 ай бұрын
what compiler did you use?
@BizzaroBrainBoi Жыл бұрын
dangggg how did you do that!!
@callowindia2 жыл бұрын
Can we need localhost for this
@oneminutecoder2 жыл бұрын
Yes. It doesn't work when I directly click and open the html file
@oneminutecoder2 жыл бұрын
In this example, i am using VS Code and Live Server Playlist for vs code kzbin.info/aero/PLrcsnb6QBvieVvHdr0X0M4uUiTSoGyAra
@bashirajasu51867 ай бұрын
But I want only one name and capital How to do that
@mr.RAND55848 ай бұрын
🇵🇭. Nice.
@TdukeTduke Жыл бұрын
I cant get it to work either. What am I missing.
@oneminutecoder Жыл бұрын
Run HTML file from within VS Code. Firstly add an HTML file Then paste this code inside it fetch("test.json") .then(response => response.json()) .then(data => showInfo(data)); function showInfo(data) { console.table(data.countries); } Add a JSON file test.json Then paste this code inside it { "countries": [ { "name": "Indonesia", "capital": "Jakarta" }, { "name": "Philippines", "capital": "Manila" } ] }
@oneminutecoder Жыл бұрын
You should see data in console. Ctrl + Shift + J
@TdukeTduke Жыл бұрын
@@oneminutecoder I got it to work now.. Thank you sir!!! I appreciate your response and commitment to help others learn!!
@mrstanley303711 ай бұрын
Can I do this with txt file?
@oneminutecoder11 ай бұрын
YES. kzbin.info9Edc1day6p8
@biswajitmohanty542 жыл бұрын
I have done the same way but fetch is not defined, could you please help
@oneminutecoder2 жыл бұрын
Do you use VS Code?
@oneminutecoder2 жыл бұрын
Maybe you are not using an HTML page and you are using Node.js
@biswajitmohanty542 жыл бұрын
@@oneminutecoder yes I am using vs code
@biswajitmohanty542 жыл бұрын
@@oneminutecoder I want to run the js file how you show on your video
@oneminutecoder2 жыл бұрын
Firstly add an HTML file
@umangternate Жыл бұрын
Anyone knows how to store data to a variable outside the fetch function?
@oneminutecoder Жыл бұрын
Do you mean this ? let countries; fetch("restcountries.com/v3.1/all") .then(response => response.json()) .then(data => { countries = data; }) .then(() => { console.log(countries); });
@umangternate Жыл бұрын
@@oneminutecoder exactly, but if I call console.log outside the fetch function, it is empty. Scope issue, I guess.
@oneminutecoder Жыл бұрын
@@umangternate Normally, for a code to run in JavaScript, previous codes must finish their work. But the fetch API doesn't work like that. Before the fetch finishes, the next code starts executing. So console.log(countries) runs before fetch brings countries. The codes waiting for each other are called synchronous, and the other is called asynchronous. That's why we use "then" in asynchronous structure. When you're done, do this. Why does fetch work asynchronously? For example, you will need to get weather forecasts from 10 different countries and you will connect to 100 different sources and get data. If you work synchronously, it will take too long. They should start fetching data at the same time without waiting for each other. This is what makes asynchronous structure necessary. fetch works like that. kzbin.info/www/bejne/rISvpmeIq7WBqJo kzbin.info/www/bejne/d6KyqGiXhbysa6M