【发布时间】:2016-11-12 13:31:39
【问题描述】:
我无法让 WebMethod 正常工作。一切都已正确设置,我已将其简化为最小的示例。
AJAX:
function DoAJAX() {
$.ajax({
type: 'POST',
url: 'faq.aspx/DoAJAX',
data: "AJAX Test",
dataType: 'text',
success: function (data, status) {
debugger;
alert(status + " " + data)
},
error: function () {
alert("error!")
}
});
}
WebMethod(在 faq.aspx.cs 中,使用 System.Web.Services;和 public static ):
[WebMethod]
public static string DoAJAX(string foo) {
return foo;
}
}
HTML(faq.aspx,带有 ScriptManager 和 EnablePageMethods)
<%@ Page Title="" Language="C#" MasterPageFile="~/MP.Master" AutoEventWireup="true" CodeBehind="faq.aspx.cs" Inherits="Lottery.faq" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
<input type="button" value="AJAX" onclick="DoAJAX()" />
</asp:Content>
点击后,AJAX 调用返回成功,data 中包含以下内容:http://pastebin.com/X0Vke0qj
DoAJAX() WebMethod 中的断点永远不会到达。
为什么不返回发送的“AJAX Test”字符串,为什么WebMethod没有命中?
【问题讨论】:
-
我刚刚试了一下,得到了同样的回复(数据 = 整个 HTML 文档)。并且 WebMethod 中的断点没有被触发。
-
在您的
web.config中是否也有ScriptModule在httpModules下注册?如果缺少它,它也将不起作用。 -
Scott:我在 web.config 中没有 ScriptModule(几乎是空的),你能详细解释一下要添加什么吗?你之前的建议也没有奏效。 (“错误!”)
-
伙计们,这对我使用 json 有用,但我也改变了其他东西。
-
当我将它添加到
中时,我收到 500.22 错误“检测到不适用于集成托管管道模式的 ASP.NET 设置。”整个 web.config:pastebin.com/tDM3AKSe
标签: c# asp.net ajax webmethod scriptmanager