Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- db
- await
- ํผํ
- mysql
- async
- eport
- slow and steady
- ๋น๋๊ธฐ
- addEventListener
- Porject
- webpack
- setTimeout()
- database
- json
- https://m.blog.naver.com/tt2t2am1118/221010125300
- sql
- ๋๊ธฐ
- promise
- Project
- Import
- execCommand
- callback
- prj
- js
- https://youtube.com/playlist?list=PLuHgQVnccGMA5836CvWfieEQy0T0ov6Jh&si=FTaYv8m21EhO-A2K
- object
- ajax
- ๊ฒ์
- ์ฐธ๊ณ ๋ธ๋ก๊ทธ
- JS #ํ๋ก์ ํธ
Archives
- Today
- Total
C-log
section28-AJAX์API ๋ณธ๋ฌธ
728x90
topic | explain | class | |
AJAX | AJAX | formํ๊ทธ์ promise๊ฐ ๋ง์ด ์๊ฐ์ด ๋๋ค. AJAX๋ ํ์ด์ง์ ๋ฐ์ดํฐ๋ฅผ ์๊ตฌํ๋ค. ํ์ด์ง๊ฐ ๋ก๋ฉ์ด ๋ ํ ๋ฐ์ดํฐ๋ฅผ ์์ฒญํ ์ ์ ๋ค. ๊ฒ์ํ ๋ ์ค์๊ฐ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์์ฒญํ ์ ์๋ค. ํ์ํ ๊ฐ๋ ์ ํฌ๋งทํ ์ ์์์ผํ๋ค. |
AJAX๊ฐ์ |
API | *์ค๋ํฌ์ธํธ ์์ฒญ์ ์ฃผ๊ณ ๋ฐ์ ์ ์๋ ํต์ (?)์๋จ(?)๊ฐ์ ๊ฒ. JASON๊ณผ ์์ฃผ ๋ฐ์ ํ ๊ด๊ณ๊ฐ ์๋ค. |
APIs๊ฐ์ | |
JASON | XML์ ๊ณผ๊ฑฐ์ ๋ง์ด ์ฌ์ฉํ๋ค. JASON.parse(data)๋ฅผ ์ด์ฉํด์ JASON๊ตฌ์กฐํ ํ๋ฉด ๋๋ค. |
JASON์ด๋? | |
//THIS IS A STRING OF JSON (NOT AN OBJECT)
const data = `{"ticker":{"base":"BTC","target":"USD","price":"11288.49813464","volume":"91769.69699773","change":"-46.29462447"},"timestamp":1596510482,"success":true,"error":""}`
// THIS IS A JS OBJECT
const parsedData = JSON.parse(data);
|
|||
POSTMAN | *Proxy : ํ๋ก์ ์๋ฒ๋ ํด๋ผ์ด์ธํธ๊ฐ ์์ ์ ํตํด์ ๋ค๋ฅธ ๋คํธ์ํฌ ์๋น์ค์ ๊ฐ์ ์ ์ผ๋ก ์ ์ํ ์ ์๊ฒ ํด ์ฃผ๋ ์ปดํจํฐ ์์คํ ์ด๋ ์์ฉ ํ๋ก๊ทธ๋จ์ ๊ฐ๋ฆฌํจ๋ค. ์๋ฒ์ ํด๋ผ์ด์ธํธ ์ฌ์ด์ ์ค๊ณ๊ธฐ๋ก์ ๋๋ฆฌ๋ก ํต์ ์ ์ํํ๋ ๊ฒ์ ๊ฐ๋ฆฌ์ผ 'ํ๋ก์', ๊ทธ ์ค๊ณ ๊ธฐ๋ฅ์ ํ๋ ๊ฒ์ ํ๋ก์ ์๋ฒ๋ผ๊ณ ๋ถ๋ฅธ๋ค. | Using Hoppscotch(or Prostman) https://hoppscotch.io/ https://www.postman.com/downloads/ {API} https://swapi.dev/ https://catfact.ninja/ |
|
๋์ฌ๋ง๋ค ์ญํ ์ด ๋ค๋ฅด๋ค. get, post, put, patch, delete, copy, head, options,link,unlink,purge,lock,unlock,propfind,view |
HTTP Verbs(๋์ฌ) | ||
*๋ฆฌ๋๋ ์
์ฟผ๋ฆฌ๋ฌธ์์ด -> 'q=' |
HTTP Status Codes https://developer.mozilla.org/en-US/docs/Web/HTTP/Status {API} https://www.tvmaze.com/api |
||
Headers : ํค๋์ ์ญํ ์? HTTP ์์ฒญ ํค๋(Headers)๋ ํด๋ผ์ด์ธํธ๊ฐ ์๋ฒ๋ก HTTP ์์ฒญ์ ๋ณด๋ผ ๋ ์ถ๊ฐ์ ์ธ ์ ๋ณด๋ฅผ ์ ๊ณตํ๋ ์ญํ ์ ํฉ๋๋ค. ์ด๋ฌํ ์ ๋ณด๋ค์ ์์ฒญ์ ๋ฉํ๋ฐ์ดํฐ๋ก์, ์์ฒญ์ ๋ชฉ์ ์ด๋ ํน์ง์ ์๋ฒ์ ์ ๋ฌํฉ๋๋ค. HTTP ์์ฒญ ํค๋์๋ ๋ค์ํ ์ ๋ณด๋ค์ด ํฌํจ๋ ์ ์์ต๋๋ค. ์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉ๋๋ ํค๋๋ ์๋์ ๊ฐ์ต๋๋ค.
|
HTTP Headers | ||
XHR | XHR์ด๋ XHR์ "XMLHttpRequest"์ ์ฝ์๋ก, JavaScript์์ ์๋ฒ์ HTTP ์์ฒญ์ ๋ณด๋ด๊ณ ์๋ต์ ๋ฐ์ ์ ์๋ API์ ๋๋ค. XHR์ ์ฌ์ฉํ๋ฉด ํ์ด์ง ์๋ก๊ณ ์นจ ์์ด๋ ์๋ฒ์ ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ์ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด, AJAX ๊ธฐ์ ์ ์ฌ์ฉํ์ฌ ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ ๋ด์ฉ์ ์๋ฒ์ ์ ์กํ๊ณ ์๋ฒ์์ ์ฒ๋ฆฌํ ๊ฒฐ๊ณผ๋ฅผ ๋ค์ ๋ฐ์์์ ํ์ด์ง๋ฅผ ๊ฐฑ์ ํ ์ ์์ต๋๋ค. ์ด๋ฅผ ํตํด ์ฌ์ฉ์ ๊ฒฝํ์ ๊ฐ์ ํ๊ณ ์๋ฒ์ ์ฒ๋ฆฌ ๋ถ๋ด์ ์ค์ผ ์ ์์ต๋๋ค. XHR์ ์ด์ Fetch API์ ๊ฐ์ ๋ค๋ฅธ API๋ก ๋์ฒด๋์ด ์ฌ์ฉ๋์ง ์๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ต๋๋ค. |
MakingXHRs | |
๋ฐ์ดํฐ ์์ฒญํ๊ณ ๊ฐ์ ธ์ค๊ธฐ const req = new XMLHttpRequest();
req.onload = function () {
console.log("IT LOADED!!")
console.log(this);
const data = JSON.parse(this.responseText);
console.log(data.name, data.height);
}
req.onerror = function () {
console.log("ERROR!!")
console.log(this);
}
req.send();
|
|||
Using The Fetch API | then |
.then(res => {
console.log("RESOLVED!", res);
res.json().then(data => console.log("JSON DONE", data));
//JSON.parse()์๋ ๋ค๋ฅด๋ค.
})
.then((data) => {
console.log(data);
})
.catch(e => {
console.log("ERROR!", e);
})
|
Using The Fetch API |
return fetch |
.then(res => {
console.log("RESOLVED!", res);
return res.json();
//JSON.parse()์๋ ๋ค๋ฅด๋ค.
})
.then((data) => {
console.log(data);
})
.then((res) => {
console.log("SECOND REQUEST RESOLVED!!");
return res.json();
})
.then((data) => {
console.log(data);
})
.then((res) => {
console.log("THIRD REQUEST RESOLVED!!");
return res.json();
})
.then((data) => {
console.log(data);
})
.catch(e => {
console.log("ERROR!", e);
})
|
||
awit |
const loadStarWarsPeople = async () => {
try {
const data = await res.json();
console.log(data);
const data2 = await res2.json();
console.log(data2);
} catch (e) {
console.log("ERROR!!", e);
}
}
|
||
AXIOS A LIBRARY FOR MAKING HTTP REQUESTS |
AXIOS | Axios๋ ํ์ฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก JS์ ๋ค์ดํฐ๋ธ ํจ์๊ฐ ์๋๋ค. Axios๋ HTTP ์์ฒญ์ ์์ฑ๊ณผ ์ฒ๋ฆฌ๋ฅผ ์ต๋ํ ๊ฐ์ํํ ๋ชฉ์ ์ผ๋ก ๋ง๋ค์ด์ก์ต๋๋ค. ๋ฐฑ๊ทธ๋ผ์ด๋์์๋ ๋์ผํ๊ฒ ๋ธ๋ผ์ฐ์ ์ fetchํจ์๋ฅผ ์ฌ์ฉํ์ง๋ง JS์์ ๊ธฐ๋ณธ ์ ๊ณตํ๋ ํจ์๊ฐ ์๋๋ฏ๋ก ์ถ๊ฐ๋ก ๋น๋ํด์ผ ํฉ๋๋ค. |
Introducing Axios https://github.com/axios/axios#installing |
const getStarWarsPerson = async (id) => {
try {
console.log(res.data);
} catch (e) {
console.log("ERROR", e);
}
}
console.log("START!!");//๋จผ์ ์ฒ๋ฆฌํ๋ค.
getStarWarsPerson(5);
|
|||
AXIOS header | JASON๊ณผ header์ ์ฐ๊ด์ฑ์? header์ ์๋ฏธ๋ฅผ ๋ชจ๋ฅด๊ฒ ์ DOM์ด ์ ํํ ๋ญ์ง? ๋ฐฐ์ ์ง๋ง... ๊ฐ๋ ์ ๋ฆฌ๊ฐ ์๋์ด ์์.. |
Axios๋ก ํด๋ ์ธํ
ํ๊ธฐ {API} https://icanhazdadjoke.com/api |
|
API๋ฅผ ๋๋ฌด ์์ฃผ ๋ฐ๋ณต(loop)์ ์ผ๋ก ํธ์ถํ๊ฑฐ๋ ์๋ํํ์ง ๋ง์์ผํ๋ค. | |||
728x90
'๐JS > โกver.0' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
section30-ํฐ๋ฏธ๋ ์๋ฒฝ ์ ๋ฆฌ (0) | 2023.04.05 |
---|---|
section29-ํ๋กํ ํ์ , ํด๋์ค, ๊ทธ๋ฆฌ๊ณ OOP(๋ฐฑ์๋๋ฅผ ๋ฃ๊ณ ๋์ค์ ๋ค์ ๋ค์ด๋ ๋ฌด๋ฐฉํ๋ค) (0) | 2023.04.04 |
section27-๋น๋๊ธฐ์ JavaScript (0) | 2023.03.30 |
section24~25-DOM์ด๋~DOM ์ด๋ฒคํธ (0) | 2023.03.24 |
section23-JavaScript์ ์ต์ ๊ธฐ๋ฅ๋ค (0) | 2023.03.24 |
Comments