简体   繁体   中英

Ajax/PageMethod both return full page HTML instead of doing the webmethod and returning properly

So I need to make a list of items with delete buttons delete their specific item. I set up an Ajax call and a webmethod in the codebehind, as I've done before in this site, but for some reason it returns the page's entire HTML again, rather than executing the webmethod and returning what I'd like it to return. Frustrated I tried to use a pagemthod instead, but it returns the same result without executing my webmethod at all.

Interestingly I have this working on several other pages already, and basically copy and pasted what I had, but for some reason on this page it doesn't work. Which means I know that it's not an issue with the server or any httpModules in the web config. I've tried the solutions I used before on my site, which were:

form1.Action = Request.RawUrl; at the top of the Page_Load function in codebehind, because of URL rewriting

PageMethods.set_path("~/Inbox.aspx"); in the javascript for the same reason as above

The below was a proposed solution to go in the web.config, but it just causes the whole server to 500. I don't see how it could help anyway though since pagemethods and ajax work on other pages on my site.

<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>

I've stripped the code down to almost nothing to make it function as basically as possible, so I'm certain there are no errors. The code always executes as successful anyway.

Here's the full code, just for context:

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

Not sure what I should do from here since the solutions that I've found work on the other pages, but not this one. Thanks in advance!

Here I am hitting my method. Please see comments in the code.

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>

Code behind

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

To anyone who stumbles upon this from the future, who knows they did everything correctly but still has it refuse to work: I found, way at the bottom of my web.config, buried beneath all of my URL rewrites, a small little <modules><remove name=""></modules> rule that disabled the ScriptModule .

Not sure how that got there, but apparently it did. As soon as I removed it, the site's webmethods started working again. I feel stupid that I was stuck on this for a whole week.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM