我的Common.js如下
//处理一般AJAX功能。function InvokeHander(url,type,data, onSuccess, onFailure) {
$.ajax({ url: url, //路径 type: type, //请求方式 data: data, //请求的数据 dataType: "json", contentType: "application/json;charset=utf-8", beforeSend: function(x) { x.setRequestHeader("Content-Type", "application/json;"); }, success: function(result) { if (onSuccess) { if (result.d != undefined) result = result.d; onSuccess(result); }
}, error: function(err) { if (onFailure) { onFailure(err); } }
})
}
aspx页面js如下
<script type="text/javascript" src="javascript/jquery-1.3.2.min.js"></script> <script type="text/javascript" src="javascript/Common.js"></script> <script type="text/javascript"> $(function() { $("#d1").change(function() {
var did = $("#d1").find("option:selected").val(); if (did != "请选择") { InvokeHander("TypeHandler.ashx","POST","{id:'"+did+"'}" , function(result) { if (result != null) { for (i = 0; i < result.length; i++) { var item = result[i]; alert(item.TypeName);
}
} }, function(err) {
alert('err'); }); } else {
} });
}); </script>
ashx文件如下:
<%@ WebHandler Language="C#" class="TypeHandler" %>
using System;using System.Web;using System.Data;using System.Data.SqlClient;using System.Collections;using System.Web.Script.Serialization;public class TypeHandler : IHttpHandler {
public void ProcessRequest(HttpContext context) {
if (context.Request["id"] == null)//这里永久为null用Request.form["id"]也不行 { } else { context.Response.Write(GetType(decimal.Parse(context.Request["id"].ToString()))); } }
public bool IsReusable { get { return false; }
}
public string GetType(decimal id) { ArrayList arr = new ArrayList(); SqlConnection conn = new SqlConnection("server=PC-200911131211;database=petsdata;uid=sa;pwd=sa123456"); SqlDataAdapter da = new SqlDataAdapter("select * from infor_type2 where infor_type2_father='" + id + "'", conn); DataSet ds = new DataSet(); da.Fill(ds); for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { arr.Add(new { TypeID = ds.Tables[0].Rows[i]["infor_type2_id"].ToString(), TypeName=ds.Tables[0].Rows[i]["infor_type2_name"].ToString() }); }
JavaScriptSerializer serializer = new JavaScriptSerializer(); string result = serializer.Serialize(arr); return result; }
}
假如我把上面请求方法换成POST传进去的data换成: "id="+did ,ashx用request.QueryString["id"]就一点问题都没。
请博客园的高手帮帮我吧。。。。我看见CSDN上也有类似的情况,好像也没解决。
地址:http://topic.csdn.net/u/20091126/17/a015820e-d41f-4689-b6fe-232a098c2699.html
11 回答
当年话下
TA贡献1890条经验 获得超9个赞
context.Request[] 是不能用json的,data数据格式改成:id=121&name=sss 等等这种格式啊应该就行了,不行的话contentType 也修改一下
context.Request.GetResponseStream() 好像是这个方法吧,他可以获取post的流,json也就可以得到啊:)
- 11 回答
- 0 关注
- 671 浏览
添加回答
举报
0/150
提交
取消