}
}
},
error: function (errorMsg) {
$(".result-table tr").remove();
$(".result-table").append("trtd城市请求出现错误 , 请您稍后重试 。。。/td/tr");
}
});
}
//加载天气
function GetWeather(cityid) {
$.ajax({
type: "POST",
url: "ashx/weatherHandler.ashx",
data: "cityid=" + cityid + "option=weather",
success: function (result) {
$(".result-table tr").remove();
var arry = result.split('|');
var obj = null;
for (var i = 0; iarry.length; i++) {
if (arry[i] != nullarry[i] != "") {
if (arry[i].indexOf(".gif")0) {
$(".result-table").append("trtdimage src='https://www.04ip.com/post/images/" + arry[i] + "'//td/tr");
}
else {
$(".result-table").append("trtd" + arry[i] + "/td/tr");
}
}
}
},
error: function (errorMsg) {
$(".result-table tr").remove();
$(".result-table").append("trtd天气数据请求出现错误,请您稍后重试 。。。/td/tr");
}
});
}
html代码:
body
form id="form1" runat="server"
div class="head-div"
table
tr
td
select class="sel-province sel"
/select
/td
td
select class="sel-city sel"
/select
/td
td
input type="button" class="btn-search" value="https://www.04ip.com/post/查询" /
/td
/tr
/table
/div
div class="result-div"
table class="result-table"
/table
/div
/form
/body
由于js不支持跨域,直接ajax+ashx一般处理程序(在里面调用天气接口) 。一般处理程序代码如下:
using System.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
namespace WeatherTest.ashx
{
/// summary
/// weatherHandler 的摘要说明
/// /summary
public class weatherHandler : IHttpHandler
{
WeatherWsClient.WeatherWSSoapClient client = new WeatherWsClient.WeatherWSSoapClient();
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string[] result = null;
string option = context.Request.Form["option"];
switch (option)
{
case "province":
result = GetProvinces();
break;
case "city":
result = GetCitys(context.Request.Form["provinceid"]);
break;
case "weather":
result = GetWeather(context.Request.Form["cityid"], null);
break;
}
string str = ConvertToString(result, option);
context.Response.Write(str);
}
/// summary
/// 数组转字符串
/// /summary
/// param name="result"/param
/// param name="option"/param
/// returns/returns
private string ConvertToString(string[] result, string option)
{
StringBuilder sb = new StringBuilder();
foreach (string item in result)
{
sb.Append(item+"|");
}
return sb.ToString();
}
/// summary
/// 省份
/// /summary
/// returns/returns
private string[] GetProvinces()
{
return client.getRegionProvince();
}
/// summary
/// 城市
/// /summary
/// param name="provinceid"/param
/// returns/returns
private string[] GetCitys(string provinceid)
{
return client.getSupportCityString(provinceid);
}
/// summary
/// 天气数据
/// /summary
/// param name="cityid"/param
/// param name="userid"/param
/// returns/returns
private string[] GetWeather(string cityid, string userid)
{
return client.getWeather(cityid, userid);
}
public bool IsReusable
{
get
{
return false;
}
推荐阅读
- pdf怎么打开是倒的,pdf文件是倒着的
- POSTGRE数据库招标技术要求,数据库招标参数
- 如何看待诚信问题的ppt,如何看待诚信这个问题
- 拍摄动作要注意什么,拍摄时的注意事项
- c语言的语句是不是函数 c语言是由函数构成的吗
- thinkphp323手册手机版,thinkphp6 手册
- 魔兽怀旧服务器电信,怀旧服电信区
- 如何做职校招生宣传ppt,职业学校招生宣传片解说词
- php如何写入数据库 php如何写入数据库内容