AJAX方式调用百度天气
后台代码:
[HttpPost]
public string AjaxWeather()
{
string CityName = string.IsNullOrEmpty(Request.Form["city"]) ? "" : Request.Form["city"].ToString(); //获取城市名称
if (CityName!="")
{
string WeatherInfo = HttpGet(string.Format("http://api.map.baidu.com/telematics/v3/weather?location={0}&output=json&ak=8f6d52bdd67cfe7b6c3db91adb29a51b", HttpUtility.UrlEncode(CityName)));
return WeatherInfo; //返回JSON
}
else
{
return "";
}
}
#region HttpGet
/// <summary>
/// Get方式取信息
/// </summary>
/// <param name="Url"></param>
/// <param name="postDataStr"></param>
/// <returns></returns>
public string HttpGet(string Url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8"; HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, System.Text.Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close(); return retString;
}
#endregion
JSON内容:
{
"error": 0,
"status": "success",
"date": "2015-04-28",
"results": [
{
"currentCity": "哈尔滨",
"pm25": "74",
"index": [
{
"title": "穿衣",
"zs": "舒适",
"tipt": "穿衣指数",
"des": "建议着长袖T恤、衬衫加单裤等服装。年老体弱者宜着针织长袖衬衫、马甲和长裤。"
},
{
"title": "洗车",
"zs": "较适宜",
"tipt": "洗车指数",
"des": "较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"
},
{
"title": "旅游",
"zs": "适宜",
"tipt": "旅游指数",
"des": "天气较好,温度适宜,但风稍微有点大。这样的天气适宜旅游,您可以尽情地享受大自然的无限风光。"
},
{
"title": "感冒",
"zs": "少发",
"tipt": "感冒指数",
"des": "各项气象条件适宜,无明显降温过程,发生感冒机率较低。"
},
{
"title": "运动",
"zs": "较适宜",
"tipt": "运动指数",
"des": "天气较好,但考虑风力较强且气温较低,推荐您进行室内运动,若在户外运动请注意防风并适当增减衣物。"
},
{
"title": "紫外线强度",
"zs": "中等",
"tipt": "紫外线强度指数",
"des": "属中等强度紫外线辐射天气,外出时建议涂擦SPF高于15、PA+的防晒护肤品,戴帽子、太阳镜。"
}
],
"weather_data": [
{
"date": "周二 04月28日 (实时:25℃)",
"dayPictureUrl": "http://api.map.baidu.com/images/weather/day/duoyun.png",
"nightPictureUrl": "http://api.map.baidu.com/images/weather/night/qing.png",
"weather": "多云转晴",
"wind": "西南风3-4级",
"temperature": "28 ~ 13℃"
},
{
"date": "周三",
"dayPictureUrl": "http://api.map.baidu.com/images/weather/day/qing.png",
"nightPictureUrl": "http://api.map.baidu.com/images/weather/night/duoyun.png",
"weather": "晴转多云",
"wind": "南风4-5级",
"temperature": "31 ~ 16℃"
},
{
"date": "周四",
"dayPictureUrl": "http://api.map.baidu.com/images/weather/day/qing.png",
"nightPictureUrl": "http://api.map.baidu.com/images/weather/night/duoyun.png",
"weather": "晴转多云",
"wind": "西风3-4级",
"temperature": "26 ~ 15℃"
},
{
"date": "周五",
"dayPictureUrl": "http://api.map.baidu.com/images/weather/day/zhenyu.png",
"nightPictureUrl": "http://api.map.baidu.com/images/weather/night/zhenyu.png",
"weather": "阵雨",
"wind": "西南风3-4级",
"temperature": "25 ~ 12℃"
}
]
}
]
}
Html代码:
城市名称:<input id="city" name="city" type="text" value="@ViewBag.City">
jQuery的Html容器<div id="WeatherHtml"></div>
jQuery代码:
<script type="text/javascript">
$(function () {
Weather();
}); function Weather() {
var _city = $("#city").val();
$.post('/WebApi/AjaxWeather', { city: _city }, function (result) {
//alert(result);
var dataObj = eval('(' + result + ')');
var html = '';
if (dataObj.status = 'success') {
$.each(dataObj.results, function (idx, item) {
html += '<div>城市名称:' + item.currentCity + '</div>';
html += '<div>PM2.5:' + item.pm25 + '</div>';
html += '<table>';
$.each(item.index, function (idx2, item2) {
html += '<tr>';
html += '<td>' + item2.tipt + '</td>';
html += '<td>' + item2.title + '</td>';
html += '<td>' + item2.zs + '</td>';
html += '<td>' + item2.des + '</td>';
html += '</tr>';
});
html += '</table>';
html += '<table>';
$.each(item.weather_data, function (idx3, item3) {
html += '<tr>';
html += '<td>' + item3.date + '</td>';
html += '<td><img src="' + item3.dayPictureUrl + '"/></td>';
html += '<td><img src="' + item3.nightPictureUrl + '"/></td>';
html += '<td>' + item3.weather + '</td>';
html += '<td>' + item3.wind + '</td>';
html += '<td>' + item3.temperature + '</td>';
html += '</tr>';
});
html += '</table>';
});
}
$("#WeatherHtml").html(html);
});
}
</script>
AJAX方式调用百度天气的更多相关文章
- PHP调用百度天气接口API
//百度天气接口API $location = "北京"; //地区 $ak = "5slgyqGDENN7Sy7pw29IUvrZ"; //秘钥,需要申请,百 ...
- asp.net 通过ajax方式调用webmethod方法使用自定义类传参及获取返回参数
实体类 public class User { public int Id { get; set; } public string Name { get; se ...
- 使用ajax和urlconnection方式调用webservice服务
<html> <head> <title>使用ajax方式调用webservice服务</title> <script> var xhr = ...
- 百度编辑器ueditor通过ajax方式提交,不需要事先转义字符的方法(异常:从客户端(xxx)中检测到有潜在危险的 Request.Form 值)
最近项目中使用百度编辑神器ueditor,确实是很好用的一款编辑器.官网教程提供的与后端数据交互都是跟表单方式有关的,项目中使用的是ajax方式提交,因此出现了不少问题,现在记录备忘下. 环境:.ne ...
- 使用jsonp跨域调用百度js实现搜索框智能提示,并实现鼠标和键盘对弹出框里候选词的操作【附源码】
项目中常常用到搜索,特别是导航类的网站.自己做关键字搜索不太现实,直接调用百度的是最好的选择.使用jquery.ajax的jsonp方法可以异域调用到百度的js并拿到返回值,当然$.getScript ...
- C#以post方式调用struts rest-plugin service的问题
struts2: 玩转 rest-plugin一文中,学习了用struts2开发restful service的方法,发现用c#以post方式调用时各种报错,但java.ajax,包括firefox ...
- SpringMvc 相关,bean map转换,百度天气,base64.js,jsBase64.java;
1. Map<String, Object>与JavaBean[POJO, Model]转换; //model public class model{ private int id; pr ...
- jquery+asp.net 调用百度geocoder手机浏览器定位--Api介绍及Html定位方法
原文来自:***/projecteactual/jqueryaspnetbaidugeocodermobilebrowserposition.html 在做一个社区项目中,支持移动浏览器进行选择地区和 ...
- 使用jsonp跨域调用百度js实现搜索框智能提示(转)
http://www.cnblogs.com/oppoic/p/baidu_auto_complete.html 项目中常常用到搜索,特别是导航类的网站.自己做关键字搜索不太现实,直接调用百度的是最好 ...
随机推荐
- Linux运维跳槽必备的40道面试精华题
过一次年,结婚.存款.父母养老,一系列向钱看的事都在碾压我们本来还挺简单的神经,但难过没有出路,唯有找到好的方法和事业方向,才能实现一步一个脚印的逆袭. 下面是一名资深Linux运维求职数十家公司总结 ...
- axis2 webService开发指南(1)
参考文件:blog.csdn.net/IBM_hoojo http://hoojo.cnblogs.com/ 1 WebService简介 WebService让一个程序可以透明的调用互联网的程序,不 ...
- 拼接sql
String whereArgs = taskTable + " where 1=1 "; if (upCheck) { whereArgs += " and type ...
- Open MSDN document directly without Visual Studio
"C:\Program Files (x86)\Microsoft Help Viewer\v2.2\HlpViewer.exe" /catalogName VisualStudi ...
- H5(1)
css布局模型 清楚了CSS 盒模型的基本概念. 盒模型类型, 我们就可以深入探讨网页布局的基本模型了.布局模型与盒模型一样都是 CSS 最基本. 最核心的概念. 但布局模型是建立在盒模型基础之上,又 ...
- ios加载本地html
UIWebView加载工程本地网页与本地图片 - (void)viewDidLoad { [super viewDidLoad]; NSString *filePath = [[NSBundle ma ...
- php static 变量声明
<?phpfunction test($key){ static $array = array(); /* 静态变量是只存在于函数作用域中的变量,注释:执行后这种变量不会丢失(下次调用这个函数 ...
- 14-python登入教务网(python+bs4)
用request先得到到session对象,用其去放送请求,会自动保存cookie. 模拟有验证码的登入步骤: 1.发送请求登入页面: 2.分析验证码的地址,以及要将登入请求发往的地址(可以先输入错的 ...
- 项目介绍4 y有用
在青岛做了两年开发,大大小小参与过三个项目的开发,一个是某公司内部的人员管理系统,一个是物流项目,最近做的是一个电商项目. 前两个项目采用的是ssh框架搭建的,最近的项目采用的是ssm框架搭建的.在实 ...
- c++ std::unordered_set
std::unordered_set template < class Key, // unordered_set::key_type/value_type class Hash = hash& ...