我正在做一些简单的测试(为一个更大的项目做准备)来调用ASP.NET WebMethod使用JQuery AJAX.在我的示例中,我的WebMethod返回一个简单的字符串.然而,当我try 使用JQuery调用它时,我会返回整个HTML页面内容,而不仅仅是字符串.我错过了什么?
客户端:
$(document).ready(function ready() {
$("#MyButton").click(function clicked(e) {
$.post("Default.aspx/TestMethod",
{name:"Bob"},
function(msg) {
alert("Data Recieved: " + msg);
},
"html"
);
});
});
服务器端:
using System;
using System.Web.Services;
namespace JqueryAjaxText
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string TestMethod(string name)
{
return "The value submitted was " + name;
}
}
}