我做一些简单的测试(准备一个更大的项目)使用JQuery AJAX调用一个ASP.NET WebMethod。在我的示例中,我的WebMethod返回一个简单的字符串。但是,当我尝试使用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;
- }
- }
- }
解决方法
查看这个链接。我使用一些他的其他职位calll WCF服务成功。请务必查看相关文章:
http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/
阅读文章,但本质上:
- $("#Result").click(function() {
- $.ajax({
- type: "POST",url: "Default.aspx/GetDate",data: "{}",contentType: "application/json; charset=utf-8",dataType: "json",success: function(msg) {
- $("#Result").text(msg.d);
- }
- });
- });