【问题标题】:AJAX don't trigger WebMethod but returns whole HTML page on successAJAX 不会触发 WebMethod,但会在成功时返回整个 HTML 页面
【发布时间】: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;
    }
}

HTMLfaq.aspx,带有 ScriptManagerEnablePageMethods

<%@ 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 中是否也有ScriptModulehttpModules 下注册?如果缺少它,它也将不起作用。
  • Scott:我在 web.config 中没有 ScriptModule(几乎是空的),你能详细解释一下要添加什么吗?你之前的建议也没有奏效。 (“错误!”)
  • 伙计们,这对我使用 json 有用,但我也改变了其他东西。
  • 当我将它添加到 中时,我收到 500.22 错误“检测到不适用于集成托管管道模式的 ASP.NET 设置。”整个 web.config:pastebin.com/tDM3AKSe

标签: c# asp.net ajax webmethod scriptmanager


【解决方案1】:

将类型更改为json,添加内容类型并在数据中,将foo参数作为json传递。

$.ajax({
    type: 'POST',
    url: 'faq.aspx/DoAJAX',
    data: "{ 'foo': 'AJAX Test' }",
    contentType: "application/json; charset=utf-8",
    dataType: 'json',
    success: function (data, status) {
        alert(status + " " + data.d)
    },
    error: function (xhr, status, error) {
        alert("error!")
    }
});

【讨论】:

  • 我复制了你的整个 AJAX 并替换了我的,然后运行它。我得到“错误!”警报。
  • 这可能是因为您在应用程序中进行了其他更改,例如在 web.config 中进行的这些编辑。请撤消所有更改。如果你仍然得到一个错误检查 xhr,我添加的状态和错误变量,并告诉你得到了什么错误。
  • 您的回答:xhr: pastebin.com/7kFLWbCd - 错误:“内部服务器错误”
  • 您是否撤消了对原始代码的更改? web方法完全一样吗?
  • 您的错误消息提示我您更改了 Web 方法中的参数名称,因此导致崩溃。您将参数称为“values”而不是“foo”。
【解决方案2】:

像这样更改您的代码:

.ASPX:

<head runat="server">
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#btnCallAjax").click(function () {
                DoAJAX();
            });

            function DoAJAX() {

                $.ajax({
                    type: "POST",
                    url: "faq.aspx/DoAJAX",
                    contentType: "application/json;charset=utf-8",
                    data: '{foo:"AJAX Test"}',
                    success: function (data, status) {
                        debugger;
                        alert(status + " " + data.d)
                    },
                    error: function () {
                        alert("error!")
                    }
                });
            }
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <input type="button" id="btnCallAjax" value="Call AJAX" />
    </form>
</body>

背后的代码:

public partial class faq : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    [WebMethod]
    public static string DoAJAX(string foo)
    {
        return foo;
    }
}

【讨论】:

  • 我复制了你的整个函数并运行它。我得到“错误!”警报。
  • 完全按照我的方式复制和粘贴代码。不要添加任何额外的逻辑或控件,它会工作。它在我这边工作得很好
猜你喜欢
  • 1970-01-01
  • 2014-06-19
  • 2014-10-10
  • 1970-01-01
  • 2010-09-25
  • 2013-04-30
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
相关资源
最近更新 更多