[英]asp.net javascript ajax error: not found
嘗試發送ajax消息失敗,並顯示以下消息:
我的簡單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代碼:
public partial class ReportViewer : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string CleanUp()
{
return "My message";
}
}
如果網址將“ ReportViewer.aspx / CleanUp”更改為“〜/ AspNetForms / ReportViewer.aspx / CleanUp”,則我捕獲到異常
“文件'/AspNetForms/~/AspNetForms/ReportViewer.aspx'不存在。”
腳本標記未正確放置在您的標記中。
嘗試這個
<%@ 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>
解決了我的問題。 只是使用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.