WEB Front-end engineers (practice) difficulty in making weather forecasts: simple and web weather forecasts
Need to prepare: jQuery Bootstrap weather forecast API (This article uses the API may be invalid, please use it flexibly)
CSS styles can be written here by yourself. Only jQuery request data and JSON data parsing are put forward.
{"Resultcode": "200", "reason": "successed! "," Result ": {" sk ": {" temp ":" 21 "," wind_direction ":" northeast wind "," wind_strength ":" Level 3 ", "humidity": "66%", "time": ""}, "today": {"temperature": "17 ℃ ~ 22 ℃ "," weather ":" Overcast "," weather_id ": {" fa ":" 02 "," fb ":" 02 "}," wind ": "northeast wind Level 3-4", "week": "Wednesday", "city": "Wuxi", "date_y": "August October 12, 2016", "dressing_index ": "More comfortable", "dressing_advice": "We recommend that you wear thin coats, denim shirts, and other clothing. Old and weak people should add appropriate clothing, and should wear jackets and thin sweaters. "," Uv_index ":" weakest "," comfort_index ":" "," pai_index ":" More suitable "," travel_index ":" More suitable "," exercise_index ": "More suitable", "drying_index": ""}, "future": {"day_20161012": {"temperature": "17 ℃ ~ 22 ℃ "," weather ":" Overcast "," weather_id ": {" fa ":" 02 "," fb ":" 02 "}," wind ": "northeast wind Level 3-4", "week": "Wednesday", "date": "20161012"}, "day_20161013": {"temperature": "17 ℃ ~ 21 ℃ "," weather ":" Overcast "," weather_id ": {" fa ":" 02 "," fb ":" 02 "}," wind ": "northeast wind Level 3-4", "week": "Thursday", "date": "20161013"}, "day_20161014": {"temperature": "18 ℃ ~ 22 ℃ "," weather ":" Shower "," weather_id ": {" fa ":" 03 "," fb ":" 03 "}," wind ": "northeast wind Level 3-4", "week": "Friday", "date": "20161014"}, "day_20161015": {"temperature": "17 ℃ ~ 22 ℃ "," weather ":" Shower "," weather_id ": {" fa ":" 03 "," fb ":" 03 "}," wind ": "northeast wind Level 3-4", "week": "Saturday", "date": "20161015"}, "day_20161016": {"temperature": "17 ℃ ~ 24 ℃ "," weather ":" Overcast "," weather_id ": {" fa ":" 02 "," fb ":" 02 "}," wind ": "northeast wind Level 3-4", "week": "Sunday", "date": "20161016"}, "day_20161017": {"temperature": "17 ℃ ~ 21 ℃ "," weather ":" Overcast "," weather_id ": {" fa ":" 02 "," fb ":" 02 "}," wind ": "northeast wind Level 3-4", "week": "Monday", "date": "20161017"}, "day_20161018": {"temperature": "17 ℃ ~ 21 ℃ "," weather ":" Overcast "," weather_id ": {" fa ":" 02 "," fb ":" 02 "}," wind ": "northeast wind 3-4", "week": "Tuesday", "date": "20161018" }}, "error_code": 0}
The above is the JSON data format to be used
The build interface is skipped and added to HTML based on the data in JSON.
When parsing data to HTML, you must pay attention to cross-origin issues.
$ (Document). ready (function (){
$. Ajax ({
Type: "POST ",
DataType: 'jsonp', // cross-Origin
Jsonp: "callback ",
JsonpCallback: "fanyi ",
Url: "http://v.juhe.cn/weather/ip", // server URL
Data: {ip: "124.126.230.180", key: "b2a208cb39cec1c93dd5534966708285"}, // request data
Success: function (datas) {// datas is the returned JSON data
If (datas. resultcode = 200) {// determine whether the value in JSON is successfully obtained based on the returned data.
$ ("# City"). text (datas. result. today. city); // parse the data to the HTML document to display the data.
$ ("# Date"). text (datas. result. sk. time + "publish ");
$ ("# Temp"). text (datas. result. sk. temp + "° ");
$ ("# Weather"). text (datas. result. today. weather );
$ ("# Jt-tq"). text (datas. result. today. weather );
$ ("# Jt-wd"). text (datas. result. today. temperature );
$ ("# Jt-fx"). text (datas. result. today. wind );
$ ("# Two-week"). text (datas. result. future. day_20161013.week );
$ ("# Two-tq"). text (datas. result. future. day_20161013.weather );
$ ("# Two-wd"). text (datas. result. future. day_20161013.temperature );
$ ("# Two-fx"). text (datas. result. future. day_20161013.wind );
$ ("# Three-week"). text (datas. result. future. day_20161014.week );
$ ("# Three-tq"). text (datas. result. future. day_20161014.weather );
$ ("# Three-wd"). text (datas. result. future. day_20161014.temperature );
$ ("# Three-fx"). text (datas. result. future. day_20161014.wind );
} Else {
// Error...
}
}
});
});