8-LABORATORIYA ISHI
Mavzu: AJAX asosida tarmoq dasturini yaratish
Ishning maqsadi:
Talabalarda Java dasturlash tilida XMLHttpRequest
obyektidan foydalanib
AJAX asosida tarmoq dasturini yaratish bo‘yicha amaliy ko‘nikmalar hosil qilish.
Nazariy qism:
AJAX (Asinxron JavaScript va XML) bu ko‘plab veb-texnologiyalardan
foydalangan holda kliyent tomonida asinxron veb-ilovalarni
yaratuvchi vositalar
to‘plami. Ajax yordamida veb-ilovalar sahifasining ko‘rinishi va ishiga xalaqit
bermasdan ma’lumotni serverdan (fonda) asinxron
ravishda yuborishi va qabul
qilishi mumkin. Ajax veb-sahifalar tarkibini dinamik ravishda butun sahifani qayta
yuklamasdan o‘zgartirishga imkon beradi.
AJAX imkoniyatlari:
-
Sahifani qayta yuklamasdan veb-sahifani yangilash;
-
Sahifa yuklangandan so‘ng serverdan ma’lumotlarni so‘rash;
-
Sahifa yuklangandan so‘ng serverdan ma’lumotlarni qabul qilish;
-
Serverga ma’lumotlarni orqa fonda yuborish.
8.1-rasm. AJAXning ishlashi
2
8.2-rasm. Ajax texnologiyasi asosida Veb ilova
XMLHttpRequest obyekti.
Barcha
zamonaviy
brauzerlar
XMLHttpRequest
obyektini
qo‘llab-
quvvatlaydi.
Fonda veb-server bilan ma’lumot almashish uchun XMLHttpRequest
obyektidan foydalanish mumkin.
Bu shuni anglatadiki, butun sahifani qayta
yuklamasdan, veb-sahifaning qismlarini yangilash mumkin.
var xhttp = new XMLHttpRequest();
3
8.1-jadval
XMLHttpRequest obyekt metodlari
Metod
Tavsifi
new XMLHttpRequest()
Creates a new XMLHttpRequest object
abort()
Cancels the
current request
getAllResponseHeaders()
Returns header
information
getResponseHeader()
Returns specific header information
open(
method, url, async, user,
psw
)
Specifies the request
method
: the
request type GET or POST
url
: the file location
async
: true (asynchronous)
or false
(synchronous)
user
: optional user name
psw
: optional password
send()
Sends the request to the server
Used for
GET requests
send(
string
)
Sends the request to the server.
Used for POST requests
setRequestHeader()
Adds a label/value pair
to the header to be sent
Serverga so‘rov jo‘natish:
-
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
-
xhttp.open("POST", "demo_post.asp", true);
xhttp.send();
-
xhttp.open("POST", "ajax_test.asp", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-
urlencoded");
xhttp.send("fname=Henry&lname=Ford");
-
xhttp.open("GET", "ajax_info.txt", false);
xhttp.send();