






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Національний університет «Львівська політехніка» Лабораторна робота на тему: «ОБ’ЄКТИ AJAX ТА СЦЕНАРІЇ ВЗАЄМОДІЇ З СЕРВЕРОМ» Виконала студентка групи ПЗ-21(з) Багрій Д.О. Перевірив Сенів М.М.
Лабораторна робота № 1 Тема: Об’єкти Ajax та сценарії взаємодії з сервером Мета: Оволодіти технологією Ajax та методами створення сценаріїв взаємодії з сервером. Порядок роботи
LR3 - Daria
BOOK STORE
Categories
Find
line-height: 1; outline: none; font-weight: 500; margin: 10px 0; } button:hover { background: #000; color: #fff; } label { margin-top: 20px; font-size: 15px; } input { max-width: 100%; border: 1px solid #e4e4e4; outline: none; height: 40px; border-radius: 6px; padding: 5px 15px; font-size: 14px; box-sizing: border-box; background-color: #fcfcfc; font-weight: 200; margin: 10px 0; } select { border: 1px solid #e4e4e4; height: 40px; border-radius: 6px; background-color: #fcfcfc; outline: none; font-size: 14px; font-weight: 500; font-style: normal; font-stretch: normal; letter-spacing: -0.08px;
color: #303030; padding: 13px 15px; line-height: 1.2; min-width: 150px; width: 230px; margin: 10px 0; } form { display: flex; flex-direction: column; } .form { max-width: 500px; margin: auto; } .container#books h1 { text-align: center; } .scrolled { position: relative; height: 320px; width: 400px; overflow: hidden; margin: auto; } .books { position: absolute; top: 0; left: 0; display: flex; } .book img { width: 100px; height: 140px; object-fit: cover;
const book = document.createElement("div"); book.className = "book"; book.innerHTML = `
${item.getElementsByTagName('title')[0].innerHTML} ${item.getElementsByTagName('price')[0].innerHTML}
${item.getAttribute('category')}
`; booksContainer.appendChild(book); }); scroll(); }; let allBooks = []; const scroll = () => { const container = getElementByClass("books"); container.style.left = `${pos}px`; pos--; setTimeout(scroll, 20); if (container.scrollWidth < -pos) {pos = 400;} }; const loadCategories = () => { var ajax = new XMLHttpRequest(); ajax.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { const resultXML = this.responseXML; allBooks = [...resultXML.getElementsByTagName("book")]; const allCategories = [...new Set(allBooks.map(book => book.getAttribute('ca tegory')))]; showCategories(allCategories); showBooks(allBooks); } }; ajax.open("GET", "books.xml", true); ajax.send(); }; const showCategories = (categories) => { const form = document.forms["bookForm"];const select = form["categories"]; categories.forEach(category => { const option = document.createElement("option"); option.setAttribute('name', category); option.innerText = category; select.appendChild(option); }) }; books.xml
Loving vs Virginia Everyday Italian 30.00
Google Google 49.99
Productavity Ninja Productavity Ninja 29.99
Green Book Green Book 39.95
Art of War Art of War
На практиці у якості відповіді сервера можна використати наступні формати: Простий текст (через responseText) Форматований HTML-текст (через responseText) JSON(через responseText) JavaScript-код (через responseText) XML(через responseXML) Наступний фрагмент коду JavaScript показує, як клієнт може використати XMLHttpRequest для запиту об'єктів в форматі JSON з серверу. // ініціалізація XMLHttpRequest xhr.onreadystatechange=function(){ if (xhr.readyState==4 && xhr.status==200){ obj=eval('( '+xhr.responseText+')'); alert ('Hello '+obj.name+' ' +obj.lastname); } } Відповідь сервера: echo '{"name":"Petro", "lastname":"Ivanenko"}'; Висновок: на даній лабораторній роботі я навчилася працювати з Ajax.