各位大侠,小弟最近研究jquery与asp.net遇到一个问题,具体前后台代码如下:
客户端:
<script type="text/javascript">
function send() {
$.ajax(
{
type: "post",
url: "Testpage.aspx",
data: { a: 1 },
success: function(result) { alert("successful!"); alert(result); }
}
);
}
</script>
服务器端:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Form["a"] == null) Response.Write("failed"); Response.End();
if (Request.Form["a"].ToString() == "1")
{
Response.Write("aaaaaaa");
Response.End();
}
}
为什么Request.Form["a"]为null 一直取不到a的值?
客户端:
<script type="text/javascript">
function send() {
$.ajax(
{
type: "post",
url: "Testpage.aspx",
data: { a: 1 },
success: function(result) { alert("successful!"); alert(result); }
}
);
}
</script>
服务器端:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Form["a"] == null) Response.Write("failed"); Response.End();
if (Request.Form["a"].ToString() == "1")
{
Response.Write("aaaaaaa");
Response.End();
}
}
为什么Request.Form["a"]为null 一直取不到a的值?
{
if (Request["a"] == null) Response.Write("failed"); Response.End();
if (Request["a"].ToString() == "1")
{
Response.Write("aaaaaaa");
Response.End();
}
}获取值是用Request[""]
function send() {
$.ajax(
{
type: "post",
url: "Testpage.aspx",
dataType: "json",
data: { a: 1 },
success: function(result) { alert(result.toString()); },
error: function(ex) { alert(ex.responseText); }
}
);
} protected void Page_Load(object sender, EventArgs e)
{
if (Request["a"] == null)
{
Response.Write("failed");
Response.End();
}
if (Request["a"].ToString() == "1")
{
Response.Write("aaaaaaa");
Response.End();
}
}
{ t: 'search'},
function(data, textStatus)
{
//处理回调数据
},
"json");主要看前端,服务器端只要Request.Forms中是可以去到数据的,注意保持编码格式一致
data:params,
或
data: "a=1",试试
所以Response.End();无论Request["a"]是否为null都会执行的
第二个if是无论如何都不会被执行到
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title> <script type="text/javascript" src="/js/jquery.js"></script> <script type="text/javascript">
function send() {
$.ajax(
{
type: "post",
url: "b.aspx",
dataType: "json",
data: { a: 1 },
success: function(result) { alert(result.toString()); },
error: function(ex) { alert(ex.responseText); }
}
);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<input type="button" onclick="send()" value="Click me" />
</form>
</body>
</html>
b.aspx.cs protected void Page_Load(object sender, EventArgs e)
{
if (Request["a"] == null)
{
Response.Write("failed");
Response.End();
}
if (Request["a"].ToString() == "1")
{
Response.Write("aaaaaaa");
Response.End();
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>