Modern Full-Stack Development



Download 5,64 Mb.
Pdf ko'rish
bet57/107
Sana06.08.2021
Hajmi5,64 Mb.
#140576
1   ...   53   54   55   56   57   58   59   60   ...   107
Bog'liq
Modern Full-Stack Development Using TypeScript, React, Node

 Getting Some Help: Axios
Rather than doing “naked” AJAX, we’ll instead use a popular library for it instead: Axios 
(
https://github.com/axios/axios
). In simplest terms, Axios is a Promise-based HTTP 
client that works in both browsers and Node. It uses XMLHttpRequest under the covers 
in a browser and uses the Node http library when used in a Node-based app. Being 
Promise-based means that you can use async/await with it (or the more “classical” 
Promise approach), which makes for a very nice API.
Axios offers some more advanced capabilities, including the ability to hook into the 
request and response cycle to make modifications broadly (think cross-cutting concerns 
in Aspect-Oriented Programming, or AOP, for things like logging and security). Or, it 
offers the ability to transform request and response data in various ways automatically 
and the ability to cancel requests, if necessary.
Chapter 9   Delivering the gooDs: MailBag, the Client


241
Using Axios also means security because it includes protection against client-side 
XSRF, or Cross-Site Request Forgery. This is a trick nefarious sorts can use to transmit 
requests to the server, masquerading as you, a legitimate user. That’s bad news, 
obviously, and Axios can keep your application safe from it without doing anything 
special on your part.
Axios has broad browser support and is as easy to use as
const response = await axios.get("your_server_url");
Or if you don’t want to use async/await (what’s wrong with you?!):
axois.get("your_server_url").then(function(response) {
  // Do something with the response.
});
Do you need to POST some data to the server? No problem:
axios.post("your_server_url",
  { firstName : "Burt", lastName : "Reynolds" }
);
Axios will automatically serialize that object into JSON for transmission to the server 
(naturally, you can pass an object reference there, it doesn’t need to be an object literal 
like that). It will also automatically deserialize a JSON response so that you have a nice 
JavaScript object to play with.
You can use any other HTTP method there too: DELETE, HEAD, OPTIONS, PUT, 
PATCH, whatever you need, it’s all there for you.
You can optionally pass a configuration object to any of the request methods after 
the URL (or even in place of the URL if the object itself contains the URL), which allows 
you to modify the requests in many ways. The options available are numerous, so I won’t 
go through them all, but a few of particular interest are the following:
• 

Download 5,64 Mb.

Do'stlaringiz bilan baham:
1   ...   53   54   55   56   57   58   59   60   ...   107




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish