jquery的 ajax方法 一直提交不到后台去
jquery的 ajax方法 一直提交不到后台去
郁闷了
方法为 :
<script type="text/javascript">
function doCheckUser() {
$.ajax({
type: "post",
url: "Default.aspx/aa",
contentType:"application/json;",
data: "{}",
success: function(msg) {
alert(msg);
}
});
}
</script>
后台代码:
[System.Web.Services.WebMethod]
public static string aa()
{
return "aa";
}
问题图: 提交后 弹出 一个 IE 框 出来 ,里面是 html 源代码
------解决方案--------------------建一个一般处理文件xxx.ashx的文件
在 ProcessRequest方法里输出context.Response.Write("aa");
然后
<script type="text/javascript">
function doCheckUser() {
$.ajax({
type: "post",
url: "xxx.ashx",
success: function(msg) {
alert(msg);
}
});
}
</script>
------解决方案--------------------不必一定要用webmethod,再说jq不支持这样的写法,原因比较复杂。可以用下面方法简单解决:
<%@ Page language="C#"%>
<script runat="server">
protected void Page_Load(object sender,EventArgs e){
Response.Charset="gb2312";
if(Request.Form["method"]=="Test")Test();
else if(Request.Form["method"]=="Test1")Test1();
else if(Request.Form["method"]=="Test2")Test2();
Response.Write("一般请求<br/>");
}
public void Test()
{
Response.Write("执行Test方法"+DateTime.Now);
Response.End();//停止其他输出
}
public void Test1()
{
Response.Write("执行Test1方法"+DateTime.Now);
Response.End();//停止其他输出
}
public void Test2()
{
Response.Write("执行Test2方法"+DateTime.Now);
Response.End();//停止其他输出
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="content-type" content="text/html;charset=gb2312" />
<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
<input type="button" value="调用Test" onclick="CallMethod('Test')"/><input type="button" value="调用Test1"
onclick="CallMethod('Test1')"/><input type="button" value="调用Test2" onclick="CallMethod('Test2')"/>
<script type="text/javascript">
function CallMethod(method){
$.ajax(
{
type: "POST",
url: "test.aspx",
data:{method:method},
success:function(msg){alert(msg);},