折腾一天,回家测试结果好好的,单位里就出问题!气氛,贴上代码,前台代码是 get与getJSON不同用法,一个需要eval下,另一个不需要把它变成对象。记一笔
需要变成对象的写法
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
function LoadCityData()
{
2
//alert("ddd")
3
$("#ddlCityName").empty();
4
$.get(
5
"Handler.ashx",
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ name: "test", age: 20 },
7![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
function(json)
{
9![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
$("#ddlCityName")[0].options.add(new Option("城市", -1));
11
12
var dataObj = eval("(" + json + ")"); //
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
$.each(dataObj.root, function(i, n)
{
14
// alert(json.root);
15
var item = new Option(n.name, n.value)
16
$("#ddlCityName")[0].options.add(item)
17
})
18
})
19
};
后台:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System;
2
using System.Web;
3![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class Handler : IHttpHandler
{
5
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void ProcessRequest (HttpContext context)
{
7
context.Response.ContentType = "text/plain";
8
9
string data="{ root: [ {name:'1',value:'0'}, {name:'6101',value:'西安市'},{name:'6102',value:'铜川市'}, {name:'6103',value:'宝鸡市'}, {name:'6104',value:'咸阳市'}, {name:'6105',value:'渭南市'}, {name:'6106',value:'延安市'}, {name:'6107',value:'汉中市'}, {name:'6108',value:'榆林市'}, {name:'6109',value:'安康市'}, {name:'6110',value:'商洛市'} ] }";
10![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
context.Response.Write(data);
13
}
14
15![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public bool IsReusable
{
16![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{
17
return false;
18
}
19
}
20![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
}
前台:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
<html xmlns="http://www.w3.org/1999/xhtml">
2
<head runat="server">
3
<title></title>
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
<script src="jquery-1.3.2-vsdoc2.js" type="text/javascript"></script>
6
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<script>![](https://www.cnblogs.com/Images/dot.gif)
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
$(document).ready(function()
{
9![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
$("#ddlProvinceName").unbind("change").change(function()
{
11![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
LoadCityData();
13
})
14![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
});
18![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
function LoadCityData()
{
21
//alert("ddd")
22
$("#ddlCityName").empty();
23
$.getJSON(
24
"Handler.ashx",
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ name: "test", age: 20 },
26![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
function(json)
{
28![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
$("#ddlCityName")[0].options.add(new Option("城市", -1));
30![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
$.each(json.root, function(i, n)
{
31
alert(json.root[i]);
32
var item = new Option(n.name, n.value)
33
$("#ddlCityName")[0].options.add(item)
34
})
35
})
36
};
37
38![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
40![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
42
43
</script>
44
</head>
45
<body>
46
<form id="form1" runat="server">
47
<div>
48
<select id="ddlProvinceName">
49
<option value="1">1</option>
50
<option value="1">1</option>
51
<option value="1">5</option>
52
<option value="1">1</option>
53
<option value="1">2</option>
54
<option value="1">4</option>
55
<option value="1">1</option>
56
<option value="1">1</option>
57
<option value="1">6</option>
58
</select>
59
60
<select id="ddlCityName">
61
</select>
62
</div>
63
</form>
64
</body>
65
</html>