【问题标题】:Ajax/PageMethod both return full page HTML instead of doing the webmethod and returning properlyAjax/PageMethod 都返回完整页面 HTML 而不是执行 webmethod 并正确返回
【发布时间】:2019-02-17 23:41:54
【问题描述】:

所以我需要使用删除按钮创建一个项目列表来删除它们的特定项目。我在代码隐藏中设置了一个 Ajax 调用和一个 webmethod,就像我之前在这个站点中所做的那样,但是由于某种原因它再次返回页面的整个 HTML,而不是执行 webmethod 并返回我希望它返回的内容.沮丧的是,我尝试改用 pagemthod,但它根本没有执行我的 webmethod 就返回了相同的结果。

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

form1.Action = Request.RawUrl; 在代码隐藏中的 Page_Load 函数顶部,因为 URL 重写

PageMethods.set_path("~/Inbox.aspx"); 在 javascript 中,原因同上

以下是在 web.config 中提出的解决方案,但它只会导致整个服务器达到 500。我看不出它有什么帮助,因为 pagemethods 和 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;
}

不确定我应该从这里做什么,因为我在其他页面上找到了解决方案,但不是这个。提前致谢!

【问题讨论】:

  • 你必须开启EnablePageMethods
  • 是的,我在页面上有一个 ScriptManager,&lt;asp:ScriptManager runat="server" ID="sm" EnablePartialRendering="true" EnablePageMethods="true" &gt;&lt;/asp:ScriptManager&gt; 在那里。
  • 我得到了我之前提到的 httpmodule 的工作,但它根本没有帮助。据说其他一些解决方案将settings.AutoRedirectMode = RedirectMode.Permanent; 更改为settings.AutoRedirectMode = RedirectMode.Off;,但这也不起作用。我不知道该怎么办,这个问题被淹没了,@CodingYoshi 请帮帮我
  • 你说你打开了EnablePageMethods,但我在你发布的代码中看不到它。怎么会?另外,如果我是你,我会创建一个全新的项目,只添加这个方法,不要连接到 db 或任何东西,看看你的方法是否有效。
  • 我没有发布所有的 HTML,只是涉及的 JS 部分,忘记了脚本管理器。页面上有很多代码,所以如果我发布整个内容,它会继续很多。对于那个很抱歉。我确实创建了一个全新的 ASPX 页面来重新开始,但它仍然返回 HTML 而不是执行 web 方法,这很令人生气,因为它适用于其他页面。我不需要创建一个新项目就可以知道这是有效的,因为相同的方法在同一个项目的其他地方也适用。

标签: javascript c# ajax webforms


【解决方案1】:

在这里,我正在使用我的方法。请看代码中的cmets。

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";
    }
}

【讨论】:

  • 当我执行 PageMethods.set_path(path); 而不是之前的操作时,控制台中出现错误。路径需要是实际的.aspx路径,而不是URL重写路径,否则找不到webmethod。这与我之前使用 webmethods 时必须做的事情相同。我确定我的代码是正确的,但无论我做什么它都不起作用,即使它在同一个项目的不同页面上工作,我不知道该怎么做。即使删除并重新制作页面也无法修复它。这让我大吃一惊
【解决方案2】:

对于任何从未来偶然发现这一点的人,他们知道他们所做的一切都是正确的,但仍然拒绝工作:我发现,在我的 web.config 的底部,埋在我所有的 URL 重写之下,一个小的禁用ScriptModule 的小&lt;modules&gt;&lt;remove name=""&gt;&lt;/modules&gt; 规则。

不确定它是如何到达那里的,但显然它确实做到了。我一删除它,网站的 webmethods 就又开始工作了。整整一个星期都被困在这个问题上,我感到很愚蠢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-09
    • 1970-01-01
    • 2012-06-02
    • 2011-08-03
    • 2021-06-02
    • 2014-06-19
    • 2015-03-25
    相关资源
    最近更新 更多