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 项目中常常用到搜索,特别是导航类的网站.自己做关键字搜索不太现实,直接调用百度的是最好 ...
随机推荐
- Delphi C++Builder RAD XE Ver 版本 官方发布时间
RAD 新版本发布时间记录 代号,官方发布时间 RIO 10.3.1,VER330,Product Ver 26 Program File 20,2019.2.14 发布 24周年 RIO 10.3, ...
- SpringData JPA 接口和方法
1.1 简单查询--接口方法 1.2 五个接口详解 1.2.1 CrudRepository接口 其中T是要操作的实体类,ID是实体类主键的类型.该接口提供了11个常用操作方法. @NoRepo ...
- sqlite在终端中输入命令不显示
问题: 今天通过命令想访问我设备里面的db文件,但是进入到 sqlite> 后,输入命令都是不显示的,但是回车是可以执行的.如图 经过一番排查后,发现,因为我前面使用了su命令,不要使用su命令 ...
- Linux ALSA声卡驱动之一:ALSA架构简介
声明:本博内容均由http://blog.csdn.net/droidphone原创,转载请注明出处,谢谢! 一. 概述 ALSA是Advanced Linux Sound Architecture ...
- SQL 组内排序
SELECT t_time, code, name, CL, row_number () OVER (partition BY t_time ORDER BY cl) AS 组内排名1, --T_ti ...
- std::mutex 引起的 C2280 尝试引用已删除的函数
起因是把之前写的类中的 mutex 使用了(之前注释掉了没用到这个变量); 或者说添加了一个 mutex 变量, 然后 这个类有嵌套在了 其类的 map 中使用, 然后 编译 就报错 ` C2280 ...
- Java方法重写与super关键字
----------siwuxie095 方法的重写: (1)在继承中也存在着重写的概念,其实就是子类定义了和父类同名的方法 (2)定义:方法名 ...
- MongoDB 数组操作
$push:向文档数组中添加元素,如果没有该数组,则自动添加数组.db.users.insert({"name":"zhang"})db.users.updat ...
- cocos2dx 触摸屏的使用
只要继承与CCLayer的类都可以实现触摸功能.CCLayer类的触摸事件的一些接口如下: // 单点触碰 virtual bool ccTouchBegan(CCTouch *pTouch, CCE ...
- Html5新瓶装老酒之一--Touch事件处理
移动端的应用越来越多的开始采用html5来实现的.Html5有许多新特性需要开发者注意,比如css3,touch事件等等.比如做一个轮播图,分析其实现要领,有三点: 1.图片的轮播效果对应的css 样 ...