|
XMLHttpRequest obyektini yaratish
|
bet | 4/4 | Sana | 18.05.2024 | Hajmi | 1,13 Mb. | | #242351 |
Bog'liq infokomM1 XMLHttpRequest obyektini yaratish:
XMLHttpRequest obyektini yaratish:
uzgaruvchi = new XMLHttpRequest();
Callback Funksiyasini yozish
Callback funktsiyasi boshqa funktsiyaga parametr sifatida uzatiladigan funktsiyadir.
Bu holda, callback funktsiyasi javob tayyor bo'lganda bajarilishi kerak bo'lgan kodni o'z ichiga olishi kerak.
xhttp.onload = function() { // Javob tor bulganda bajariladigan kodlar }
So’rovni yuborish
Serverga so‘rov yuborish uchun XMLHttpRequest obyektining open() va send() metodlaridan foydalanamiz:
xhttp.open("GET", “malumot.txt"); xhttp.send();
Ajax Server so'rovlari uchun GET va POSTdan foydalanishingiz kerak Get va Postning maqsadi
GET nomi ishlatilgani sababli, ma'lumot olish uchun. u axborotni o'qiyotganingizda foydalanish uchun mo'ljallangan. Brauzerlar GET so'rovidan natijani keshlashadi va agar bir xil Get so'rovi qayta tuzilgan bo'lsa, ular butun so'rovni qayta ishlashdan ko'ra, keshlangan natija ko'rsatadilar.
Bu brauzerni ishlashda nuqson emas; GET qo'ng'iroqlarini yanada samaraliroq qilish uchun mo'ljallangan tarzda ishlash uchun mo'ljallangan. GET chaqiruvi faqat ma'lumotni qabul qilish; bu serverda hech qanday ma'lumotni o'zgartirishni nazarda tutmaydi, shuning uchun ma'lumotlarni qayta talab qilish bir xil natijalarni qaytarishi kerak.
Post metodi serverdagi ma'lumotlarni yozish yoki yangilash uchun. Ushbu turdagi qo'ng'iroqlarning ma'lumotlari o'zgarishi kutiladi, shuning uchun ikki xil POST qo'ng'iroqlaridan olingan natijalar juda yaxshi bir-biridan farq qilishi mumkin.
Ikkinchi POST chaqiruvidan oldin dastlabki qadriyatlar dastlabki qadriyatlar avvalgi qadriyatlardan farq qiladi, chunki dastlabki qo'ng'iroq bu qiymatlardan kamida bittasini yangilaydi. Shuning uchun Post chaqiruvi oldindan javobning keshlangan nusxasini saqlab turish o'rniga serverdan javobni oladi.
Get yoki Postni qanday tanlash mumkin
Ajax chaqiruvingizdan o'tgan ma'lumotlar miqdori asosida Get va Post o'rtasida tanlov o'rniga, Ajax qo'ng'iroqlari aslida nima qilayotganiga qarab tanlashingiz kerak.
Agar qo'ng'iroq serverdan ma'lumotlarni olish uchun bo'lsa, GET dan foydalaning. Qabul qilinishi kerak bo'lgan qiymatning boshqa jarayonlarning yangilanishi natijasida vaqt o'tishi bilan o'zgarishi kutilsa, keyingi qo'ng'iroqlar natijasida oldingi keshlangan nusxa ishlatilmasligi uchun Get qo'ng'iroqingizda o'tganingiz uchun joriy vaqt parametri qo'shing bu endi to'g'ri emas.
interface AuthApi {
@POST("auth/sign-up")
suspend fun signUp(@Body signUpEntity: SignUpEntity): SignUpResponse
@POST("sign-up/resend")
suspend fun signUpResent(@Body signUpResentEntity: SignUpResentEntity): Response
@GET("cards")
suspend fun getCards(@Header("Authorization") bearerToken: String): GetCardsespons
@DELETE("cards/{id}")
suspend fun deleteCards(
@Path("id") id: String,
@Header("Authorization") bearerToken: String
): Response
@GET("history")
suspend fun history(@Header("Authorization") bearerToken: String): Response
@GET("history?page=:id")
suspend fun historyByCard(
@Header("Authorization") bearerToken: String,
id: Int
): Response
@PUT("cards/{cardId}")
suspend fun updateCard(
@Path("cardId") cardId: String,
@Body cardNameUpdate: UpdateCardEntity,
@Header("Authorization") bearerToken: String
): Response
}
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/Sign_in_Screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
android:id="@+id/welcome_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="113dp"
android:fontFamily="@font/poppins"
android:text="Welcome Back!"
android:textColor="#414A61"
android:textSize="35sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/poppins_light"
android:text="Sign in to continue "
android:textColor="#414A61"
android:textSize="15sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/welcome_back" />
android:layout_width="360dp"
android:layout_height="wrap_content"
android:layout_marginTop="297dp"
android:orientation="vertical"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
android:id="@+id/lay2"
android:layout_width="338dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:hint="Password"
app:boxCornerRadiusBottomEnd="10dp"
app:boxCornerRadiusBottomStart="10dp"
app:boxCornerRadiusTopEnd="10dp"
app:boxCornerRadiusTopStart="10dp"
app:boxStrokeColor="@color/custom_border_color"
app:endIconMode="password_toggle"
app:hintTextColor="@color/black"
>
android:id="@+id/signin"
android:layout_width="333dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_marginTop="100dp"
android:background="@drawable/button_background"
android:fontFamily="@font/poppins"
android:text="Sign in My Account"
android:textColor="#FFFFFF" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:orientation="horizontal">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Don’t have an account? " />
Foydalanilgan Adabiyotlar:
Mozilla Developer Network (MDN) AJAX Documentation:
https://developer.mozilla.org/en-US/docs/Web/Guide/AJAX
jQuery AJAX Documentation:
https://api.jquery.com/category/ajax/
W3Schools AJAX Tutorial:
https://www.w3schools.com/xml/ajax_intro.asp
|
| |