Ajax / PageMethod都返回整页HTML,而不是执行webmethod并正确返回

时间:2019-02-17 23:41:54

标签: javascript c# ajax webforms

因此,我需要使用删除按钮制作一个项目列表,以删除其特定项目。正如我在此站点之前所做的那样,我在后面的代码中设置了一个Ajax调用和一个Web方法,但是由于某种原因,它再次返回了页面的整个HTML,而不是执行该Web方法并返回了我想返回的内容。沮丧的是,我尝试使用pagemthod代替,但是它返回了相同的结果,而根本没有执行我的web方法。

有趣的是,我已经在其他几个页面上进行过此工作,并且基本上复制并粘贴了我所拥有的内容,但是由于某些原因,在此页面上它不起作用。这意味着我知道这与服务器或Web配置中的任何httpModules无关。我已经尝试过在网站上使用过的解决方案,这些解决方案是:

form1.Action = Request.RawUrl;位于代码背后的Page_Load函数顶部,因为URL重写

PageMethods.set_path("~/Inbox.aspx");在JavaScript中的原因与上述相同

以下是在web.config中使用的建议解决方案,但它只会使整个服务器达到500。尽管页面方法和Ajax在我网站的其他页面上都可以工作,但我仍然看不到它有什么帮助。

<system.web>
  <httpModules>
    <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
  </httpModules>
</system.web>

我已将代码精简为几乎没有,以使其尽可能基本地起作用,因此,我确定没有错误。无论如何,代码总是成功执行。

这是完整的代码,仅用于上下文:

HTML / JS:

<form id="form1" runat="server" style="text-align:left">
        <asp:ScriptManager runat="server" ID="mailSM" EnablePageMethods="true" EnablePartialRendering="true" ClientIDMode="Static" ></asp:ScriptManager>
        <input runat="server" id="maillist" type="hidden" value="" />
        <input runat="server" id="inboxhf" type="hidden" value="1" />
        <div id="mail-collection" class="main-container">
            <a id="back" class="buttons" href="/">back</a><a class="buttons" href="/sendmail">send new mail</a><a id="box" class="buttons" href="/outbox">your sent mail</a>
        </div>

        <script type="text/javascript">
            function DeleteMail(mailID, fromID, toID) {
                let pagepath = window.location.pathname.substr(1);

                PageMethods.set_path("Mailbox.aspx");
                PageMethods.DeleteMailCB(fromID, toID, pagepath, mailID, OnSuccess, OnError);
            }

            function OnSuccess(response) {
                alert(response);
                location.reload();
            }

            function OnError(response) {
                alert("e" + response);
                //OnSuccess();
            }
        </script>
    </form>

C#:

[WebMethod]
public static void DeleteMail(string fromID, string toID, string pagepath, string mailID)
{
    DB.TestForSomething();
    //above just does a basic INSERT straight into a test table in the 
    //database. since nothing is being inserted, I know this webmethod 
    //isn't getting used at all.

    return;
}

由于我发现的解决方案可以在其他页面上使用,因此不能确定我该从这里开始做什么,但不能在此页面上使用。预先感谢!

2 个答案:

答案 0 :(得分:0)

我在这里打我的方法。请查看代码中的注释。

ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="FredWebForm.WebForm4" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" language="javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
    <script type="text/javascript">
        function DeleteMail(mailID, fromID, toID) {
            let path = window.location.pathname.substr(1);
            //"inbox" or "outbox"

            //changed path for me
            //PageMethods.set_path("~/Inbox.aspx");<---did not work
            //What you had did not work
            PageMethods.set_path(path);
            PageMethods.DeleteMail(fromID, toID, path, mailID, OnSuccess, OnError);
        }
        function OnSuccess(response) {
            alert(response);
            //remarked out initially
            location.reload();
        }
        function OnError() {
            //OnSuccess();
        }
        $(function () {
            $("#theButton").click(function () {
                DeleteMail(12, 14, 16);
            })
        })
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager runat="server" ID="sm" EnablePartialRendering="true" EnablePageMethods="true">
        </asp:ScriptManager>
        <div>
            <input type="button" value="Go" id="theButton" />
        </div>
    </form>
</body>
</html>

后面的代码

//I am using WebForm4, but you can use a different page name
public partial class WebForm4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        form1.Action = Request.RawUrl;
    }

    [WebMethod]
    public static string DeleteMail(string fromID, string toID, string pagepath, string mailID)
    {
        //DB.TestForSomething();
        //above just does a basic INSERT straight into a test table in the 
        //database. since nothing is being inserted, I know this webmethod 
        //isn't getting used at all.

        return "SomeData";
    }
}

答案 1 :(得分:0)

对于那些从未来偶然发现此问题的人,他们知道自己做的一切正确,但仍然无法正常工作:我发现,在我的web.config底部,埋藏在我所有URL重写的下面,禁用<modules><remove name=""></modules>的小ScriptModule规则。

不确定那是怎么实现的,但显然做到了。删除它后,该站点的网络方法再次开始工作。我呆了整整一个星期都觉得很愚蠢。

相关问题