Ajax effect of weather forecast

Source: Internet
Author: User
Tags string to json

Recently, I saw a lot of real-time weather forecasts on my website, which is very practical and has a good user experience. It also provides great help to users. Users can learn real-time weather information through your website. I felt quite interesting, so I studied the implementation methods. So I decided to share the Code with you, hoping to contribute to your front-end business.

The principle is actually very simple. When the page is loaded, the PHP file is requested and a group of json is returned. This set of json is the weather information we need. We only need to parse the returned json and put it in the corresponding position.

JavaScript code:


[Javascript]
$ (Function (){

$. Ajax ({
// Request address
Url: "weather. php ",
// The function executed after the request is successful
Success: function (data ){
 
// Parse the returned data with eval () and convert the string to JSON format
Var oD = eval ('+ data + ')');

// Get element with jquery-1.8.2
Var $ place = $ (". place "),
$ Temp = $ (". temp "),
$ Wind = $ (". wind "),
$ WindPower = $ (". windPower ");

// Place the returned data to the corresponding location
Using place.html (oD ["weatherinfo"] ["city"]);
$Temp.html (oD ["weatherinfo"] ["temp"] + "° ");
$Wind.html (oD ["weatherinfo"] ["WD"]);
Export windpower.html (oD ["weatherinfo"] ["WS"]);
 
}
});

})

$ (Function (){
 
$. Ajax ({
// Request address
Url: "weather. php ",
// The function executed after the request is successful
Success: function (data ){

// Parse the returned data with eval () and convert the string to JSON format
Var oD = eval ('+ data + ')');

// Get element with jquery-1.8.2
Var $ place = $ (". place "),
$ Temp = $ (". temp "),
$ Wind = $ (". wind "),
$ WindPower = $ (". windPower ");

// Place the returned data to the corresponding location
Using place.html (oD ["weatherinfo"] ["city"]);
$Temp.html (oD ["weatherinfo"] ["temp"] + "° ");
$Wind.html (oD ["weatherinfo"] ["WD"]);
Export windpower.html (oD ["weatherinfo"] ["WS"]);

}
});
 
})

 

Because data is obtained from other websites, I use a little bit of php knowledge. The idea of PHP is to use the file_get_contents () function to request the specified address and output the returned data to the foreground.

PHP code:


[Php]
<? Php
$ Weather = file_get_contents ("http://www.weather.com.cn/data/sk/101010100.html ");
Echo $ weather;
?>

<? Php
$ Weather = file_get_contents ("http://www.weather.com.cn/data/sk/101010100.html ");
Echo $ weather;
?>


HTML code:


[Html]
<Div class = "all">
Here is: <span class = "place"> city </span>,
The temperature is <span class = "temp"> temperature </span>,
Wind direction: <span class = "wind"> wind direction </span>,
Wind power: <span class = "windPower"> Wind Power </span>
</Div>

<Div class = "all">
Here is: <span class = "place"> city </span>,
The temperature is <span class = "temp"> temperature </span>,
Wind direction: <span class = "wind"> wind direction </span>,
Wind power: <span class = "windPower"> Wind Power </span>
</Div>


Add some styles to make the page clearer.

Css code:


[Css]
 
. All span {font: bold 30px/50px ""; color: red ;}


. All span {font: bold 30px/50px ""; color: red ;}

 

The above is the simple Weather Forecast effect, and there are some simple principles for implementation. I hope it will be helpful to everyone's front-end business. The above is only my understanding. If there are minor problems, we can communicate and learn from each other at any time.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.