简体   繁体   English

asp.net javascript ajax错误:找不到

[英]asp.net javascript ajax error: not found

Trying to send ajax message fails with message: 尝试发送ajax消息失败,并显示以下消息:

在此处输入图片说明

My simple aspx page: 我的简单aspx页面:

<%@ 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: CS代码:

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 如果网址将“ ReportViewer.aspx / CleanUp”更改为“〜/ AspNetForms / ReportViewer.aspx / CleanUp”,则我捕获到异常

"The file '/AspNetForms/~/AspNetForms/ReportViewer.aspx' does not exist." “文件'/AspNetForms/~/AspNetForms/ReportViewer.aspx'不存在。”

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 只是使用asp:button而不是简单的button和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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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