日期:2014-05-16  浏览次数:20672 次

ashx文件怎么获取$.ajax()方法发送的json数据
$.ajax({
  type: "POST",
  url: "handler.ashx",
  data: "{name:'jack'}",
  dataType:"json",
  contentType:"application/json",
  success: function(msg){ alert( "Data Saved: " + msg.name ); } 
  }); 

在handler.ashx文件中用
string name = context.Request.Params["name"];
context.Response.ContentType = "application/json";
HttpContext.Current.Response.Write("{name:'" + name + "'}");
不行获取不到数据

alert 显示的时候msg.name为空


------解决方案--------------------
既然是post提交的,try
C# code
string name = context.Request.Form["name"];

------解决方案--------------------
post方式提交的是使用 context.Request.Form 获取数据
获取不到的原因也有可能是你的url路径不对 设断点断下 看看能不能跳到这个handler.ashx就知道了
------解决方案--------------------
C# code

string name = context.Request.Params["name"].ToString();

context.Response.Write("结果:" + name);

------解决方案--------------------
这个问题我也遇到了.你在获取数据时应该这样:
string items = Request["items"];