2016-11-16 83 views
0

尝试发送Ajax消息失败消息:asp.net JavaScript的Ajax错误:未找到

enter image description here

我简单的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"; 
    } 

} 

如果url将“ReportViewer.aspx/CleanUp”更改为“〜/ AspNetForms/ReportViewer.aspx/Clea nUp“然后我发现异常

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

+0

尝试'数据:{}' –

+0

相同的结果,即使删除“数据”线,坚持错误实际上 – Ramunas

+0

你原来的代码更正确w.r.t.数据 - 尽管我认为jquery会对数据进行jsonify –

回答

0

解决我的问题。只是用ASP:按钮,而不是简单的按钮和阿贾克斯

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

脚本标记被错误地放置在您的标记中。

试试这个

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

+0

脚本或不解决问题 – Ramunas