我之前的aspx 页面引用了脚本文件area.js(这里包含了区,地名,高校的信息为JSon表达式):
- HTML code
<script type="text/javascript" src="area.js" charset="GB2312"></script>
它的下面还有两个脚本文件链接:
- HTML code
<script type="text/javascript" src="search-results.js" charset="GB2312"></script> <script type="text/javascript" charset="GB2312" src="header.js"></script>
可以访问 area.js 。
area.js格式为(部分):
- JScript code
var zone = [{ "zone_id": "15", "city_id": "12", "zone_name": "朝阳区"},{ "zone_id": "16", "city_id": "12", "zone_name": "东城区"}]var area = [{ "area_id": "22", "area_name": "\u4e09\u91cc\u5c6f", "zone_id": "15", "area_x": 116.45345, "area_y": 39.93452 },{ "area_id": "23", "area_name": "\u5b89\u8d1e", "zone_id": "15", "area_x": 116.40453, "area_y": 39.96906}]var school = [{ "school_id": "4", "school_name": "\u6e05\u534e\u5927\u5b66", "zone_id": "17", "school_x": 116.32608, "school_y": 40.00161 },{ "school_id": "5", "school_name": "\u5317\u4eac\u5927\u5b66", "zone_id": "17", "school_x": 116.30605, "school_y": 39.99314}]
数据量因为不多,所以之前人做的就直接把这个area.js当数据库,直接引用zone,area,school 的json表达式,脚本链接放在页面里,在他下面的其他脚本文件可以直接获取这三个字段并获得json表达式。后来用动态的(数据库提取)。我采用ashx文件(area.ashx)测试把上面的压缩成一个字符串(str):
- C# code
context.Response.ContentType = "text/javascript"; context.Response.Write(str);
仍然这样访问:
- HTML code
<script type="text/javascript" src="area.ashx" charset="GB2312"></script>
要怎么才能它成一个可访问的类似脚本文件。??
------解决方案--------------------------------------------------------
没有这种写法的
用ajax访问ashx页面,获取json对象
------解决方案--------------------------------------------------------
------解决方案--------------------------------------------------------
ajax接收json字符串
context.Response.ContentType = "application/json";
stringbuilder构造json字符串输出
$.ajax({
type: "POST",
url: "../ajax/Handler.ashx",
dataType: "text",
data: "meth=load",
success: function(msg) {
alert(msg);
$("#q")(msg);
}
});
或 object转json
------解决方案--------------------------------------------------------
用aspx就行了 里边把其它的html都去掉,只有js就行了
------解决方案--------------------------------------------------------