<script language="javascript" type="text/javascript">
$(document).ready(function(){
$("#Button1").click(function(){
$.ajax({
type:"post",
dataType:"text",
url:"Handler.ashx",
data:"username"+$("#Text1").val(),
success:function(msg){
alert(msg);
}
})
})
})
</script>
ashx:
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string name = string.Empty;
if (context.Request["username"] != null)
{
name = context.Request["username"].ToString();
}
context.Response.Write(name);
}
在这里我怎么获取不到username的值呢?
格式不对吧,data:"username"+$("#Text1").val(),
应该是username=***&other=***, 看一下Jquery的官方文档ajax部分,注意文本内容还需要编码,最好换一种{***, ***}的形式
data:"username"+$("#Text1").val()=>data:{"username:"+$("#Text1").val()}
data:"username"+$("#Text1").val() 写错了
改为:data:"username="+$("#Text1").val(),
你这写的逻辑也太乱了吧
var txt = $("#Text1");
data:"username"+$("#Text1").val() 改为data:"{'"username':'"+txt.val()+"'}",
public void ProcessRequest (string username) {
response.write(username);
}