Home >Web Front-end >JS Tutorial >Discover the versatility of Ajax
Exploring the versatility of Ajax requires specific code examples
Introduction:
In modern web development, Ajax (Asynchronous JavaScript and XML) has become One of the indispensable key technologies. Through Ajax, we can realize asynchronous data interaction between the web page and the server, making the user experience of the web page more smooth and efficient. This article will delve into the versatility of Ajax and provide some specific code examples to help readers better understand and apply Ajax technology.
1. Basic principles of Ajax technology
The basic principle of Ajax is to use JavaScript and XMLHttpRequest objects to achieve asynchronous communication between the browser and the server. Without refreshing the entire page, Ajax can send a request to the server, obtain data, and dynamically update the data to the web page. This makes web pages real-time and dynamic.
2. Common application scenarios of Ajax
3. Specific code examples
Form data verification:
var usernameInput = document.getElementById('username'); var checkUsernameBtn = document.getElementById('checkUsername'); checkUsernameBtn.addEventListener('click', function() { var username = usernameInput.value; var xhr = new XMLHttpRequest(); xhr.open('GET', '/checkUsername?username=' + username, true); xhr.onreadystatechange = function() { if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) { var response = JSON.parse(xhr.responseText); if (response.isValid) { // 提示用户名可用 } else { // 提示用户名已被占用 } } } xhr.send(); });
Real-time search:
var searchInput = document.getElementById('search'); var searchResults = document.getElementById('search-results'); searchInput.addEventListener('input', function() { var keyword = searchInput.value; var xhr = new XMLHttpRequest(); xhr.open('GET', '/search?keyword=' + keyword, true); xhr.onreadystatechange = function() { if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) { var response = JSON.parse(xhr.responseText); // 在搜索结果容器中显示搜索结果 searchResults.innerHTML = response.results; } } xhr.send(); });
Dynamic loading of content:
var loadMoreBtn = document.getElementById('load-more'); var postsContainer = document.getElementById('posts'); loadMoreBtn.addEventListener('click', function() { var page = Number(loadMoreBtn.dataset.page); var xhr = new XMLHttpRequest(); xhr.open('GET', '/getPosts?page=' + page, true); xhr.onreadystatechange = function() { if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) { var response = JSON.parse(xhr.responseText); // 将新的帖子添加到页面上 response.posts.forEach(function(post) { var postElement = document.createElement('div'); postElement.innerHTML = post.title; postsContainer.appendChild(postElement); }); loadMoreBtn.dataset.page = page + 1; } } xhr.send(); });
Conclusion:
The above are some practical application scenarios and specific code examples of the versatility of Ajax. Through Ajax, we can realize form data verification, real-time search, dynamic content loading and other functions, which greatly improves the user experience of web pages. I hope the content of this article can help readers better understand and apply Ajax technology.
The above is the detailed content of Discover the versatility of Ajax. For more information, please follow other related articles on the PHP Chinese website!