Ramunas Ramunas - 19 days ago 6
Ajax Question

asp.net javascript ajax error: not found

Trying to send ajax message fails with message:

enter image description here

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

Answer

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>