AJAX Weather Forecast Frontend
AJAX (Asynchronous JavaScript and XML) is a technology that allows exchanging data with the server and updating parts of the webpage without the need to reload the entire page. By using AJAX, real-time weather forecast functionality can be realized, providing users with a more convenient experience in querying weather information.
1. HTML: for building webpage structure
2. CSS: for styling the webpage
3. JavaScript: for implementing AJAX requests and handling server data
4. AJAX: for asynchronous interactions with the server
5. JSON: for data exchange between server and client
1. Create HTML structure
2. Write CSS styles
3. Implement AJAX requests using JavaScript
4. Handle server data and update page content
1. Create HTML structure
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF8"> <title>AJAX Weather Forecast</title> <link rel="stylesheet" href="style.css"> </head> <body> <h1>Weather Forecast</h1> <div id="weather"> <p>City: <span id="city"></span></p> <p>Temperature: <span id="temperature"></span>℃</p> <p>Weather: <span id="weather_info"></span></p> </div> <button id="getWeather">Get Weather</button> <script src="script.js"></script> </body> </html>
2. Write CSS styles
body { font-family: Arial, sans-serif; } #weather { margin-top: 20px; }
3. Implement AJAX requests using JavaScript
document.getElementById('getWeather').addEventListener('click', function() { var city = prompt('Enter city name:'); if (city) { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { var data = JSON.parse(xhr.responseText); document.getElementById('city').innerText = data.city; document.getElementById('temperature').innerText = data.temperature; document.getElementById('weather_info').innerText = data.weather_info; } else if (xhr.readyState == 4) { alert('Failed to get weather information. Please check the network connection or the city name.'); } }; xhr.open('GET', 'https://api.example.com/weather?city=' + city, true); xhr.send(); } else { alert('Enter city name!'); } });
4. Handle server data and update page content
// Assuming the server returns the data in the format: {"city": "Beijing", "temperature": "25", "weather_info": "Sunny"} var data = {"city": "Beijing", "temperature": "25", "weather_info": "Sunny"}; document.getElementById('city').innerText = data.city; document.getElementById('temperature').innerText = data.temperature; document.getElementById('weather_info').innerText = data.weather_info;
Of course, below is a simple HTML introduction example using AJAX to fetch weather forecast data from the server and display it. Please note that it only provides the frontend HTML and JavaScript parts; you still need a backend service to provide the weather forecast data for the AJAX request.
``html
<!DOCTYPE html>
<html lang="zhCN">
<head>
<meta charset="UTF8">
<title>AJAX Weather Forecast Frontend</title>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 10px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<h2>Weather Forecast</h2>
<table id="forecastTable">
<thead>
<tr>
<th>Date</th>
<th>Weather</th>
<th>Temperature</th>
</tr>
</thead>
<tbody id="forecastTableBody"><! Weather forecast data will be dynamically filled through AJAX here ></tbody>
</table>
</body>
</html>
``
Please note that in actual use, you need to replace the value of the `url` variable with the actual backend API endpoint, and ensure that the data returned by the backend matches the `data` object in the above code. Also, as jQuery library is used here, ensure that jQuery library is included in your HTML page. In the example above, I used jQuery provided by a CDN.
Thank you for reading. Feel free to comment, follow, like, and share. Your support is appreciated!
```