前台:
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnOK" runat="server" Text="Button" />
</div>
</form>
<script type="text/javascript">
$(function () {
$("#btnOK").click(function () {
alert("1");
$.ajax({
//要用post方式
type: "Post",
//方法所在页面和方法名
url: "WebForm1.aspx/SayHello",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
//返回的数据用data.d获取内容
alert(data.d);
},
error: function (err) {
alert(err);
}
});
//禁用按钮的提交
return false;
});
});
</script>
</body>
后台:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
namespace WebApplication2
{
public partial class WebForm1 : System.Web.UI.Page
{
[WebMethod]
public static string SayHello()
{
return "Hello Ajax!";
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
点了这个按钮之后没反应。
要的结果是这样的:
大家帮帮忙啊。
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnOK" runat="server" Text="Button" />
</div>
</form>
<script type="text/javascript">
$(function () {
$("#btnOK").click(function () {
alert("1");
$.ajax({
//要用post方式
type: "Post",
//方法所在页面和方法名
url: "WebForm1.aspx/SayHello",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
//返回的数据用data.d获取内容
alert(data.d);
},
error: function (err) {
alert(err);
}
});
//禁用按钮的提交
return false;
});
});
</script>
</body>
后台:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
namespace WebApplication2
{
public partial class WebForm1 : System.Web.UI.Page
{
[WebMethod]
public static string SayHello()
{
return "Hello Ajax!";
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
}

要的结果是这样的:

大家帮帮忙啊。