简体   繁体   中英

asp.net javascript ajax error: not found

Trying to send ajax message fails with message:

在此处输入图片说明

My simple aspx page:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ReportViewer.aspx.cs" Inherits="MyProject.AspNetForms.ReportViewer" %>
<script type ="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type ="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
        function CleanUpSession() {
            $.ajax({
                type: "POST",
                url: "ReportViewer.aspx/CleanUp",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: "{}",
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
                },
                success: function (result) {
                    alert("We returned: " + result);
                }
            });
        }
    </script>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<button OnClick="CleanUpSession();">test</button>
</body>
</html>

cs code:

public partial class ReportViewer : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    [WebMethod]
    public static string CleanUp()
    {
        return "My message";
    }

}

If url changed "ReportViewer.aspx/CleanUp" to "~/AspNetForms/ReportViewer.aspx/CleanUp" then i catch exception

"The file '/AspNetForms/~/AspNetForms/ReportViewer.aspx' does not exist."

The Script tag is placed incorrectly in your markup.

Try this

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ReportViewer.aspx.cs" Inherits="stackoverflow1.ReportViewer" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <script type ="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script> <script type ="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script type="text/javascript"> function CleanUpSession() { $.ajax({ type: "POST", url: "ReportViewer.aspx/CleanUp", contentType: "application/json; charset=utf-8", dataType: "json", data: "{}", error: function (XMLHttpRequest, textStatus, errorThrown) { alert("Request: " + XMLHttpRequest.toString() + "\\n\\nStatus: " + textStatus + "\\n\\nError: " + errorThrown); }, success: function (result) { alert("We returned: " + result); } }); } </script> <title></title> </head> <body> <button OnClick="CleanUpSession();">test</button> </body> </html> 

Solved my problem. Just used asp:button instead of simple button and ajax

<body onbeforeunload="CleanUpSession();">
<script type="text/javascript">
function CleanUpSession() {
    var item = document.getElementById('<%=ButtonCleanUp.ClientID%>');
    if (item != null)
        item.click();
}
    </script>
<form id="form1" runat="server">
<asp:Button ID="ButtonCleanUp" OnClick="CleanUp_Click" Text="Button Text" runat="server"/>
</form>
</body>

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