簡體   English   中英

無法使用ajax asp.net jquery調用Web方法

[英]unable to call web method using ajax asp.net jquery

我正在嘗試使用ajax從jquery調用asp方法,但出現錯誤,未知的Web方法參數方法名稱

Default.aspx:jQuery代碼

 <%@ Page Title="Home Page" Language="C#"
 MasterPageFile="~/Site.master" AutoEventWireup="true"
     CodeBehind="Default.aspx.cs" Inherits="LMSReportingDashboard._Default" %


 <asp:Content ID="SideBar" runat="server"
 ContentPlaceHolderID="SideBar" <asp:ScriptManager ID="scripman1"
 runat="server" EnablePageMethods="True"</asp:ScriptManager

 <ul id="sidebarlist" runat="server"
      <li class="selected"<a href='#'<spanAll</span</a</li
      <li<a href='#'<spanFavourite</span</a</li
      <li<a href='#'<spanBookmark</span</a</li
      <li id="category" <spanCategory</span</li
      </ul </asp:Content

 <asp:Content ID="BodyContent" runat="server"
 ContentPlaceHolderID="MainContent"

 <script type="text/javascript"




     /* Updating lable value depending on selected category */
     $(document).ready(function () {
         $(".left_column ul li").click(function () {
             var control = $(this);
             var selected = $(this).find("span").text();
             $("#stream-title").text(selected);

             /* Calling Page Method */
             $.ajax({
                 'type': 'POST',
                 'url' : 'Default.aspx/UpdateReportStream',
                 'data': '{' + "'category':" + selected + '}',
                 'contentType': "application/json",
                 success: function (msg) {

                 },
                 error: function (msg) {
                     alert(msg);
                 }
             });

         });
     });

     </script

   <header class="report-stream-header"   <h2 id="stream-title"
 class="stream-title"All</h2   </header
      <ul id="report_stream_list" runat="server" class="report_list" 
        </ul    </asp:Content

Default.aspx.cs

  [System.Web.Services.WebMethod]
         public static void UpdateReportStream(string category)
         {
             //do something

         }

在執行代碼時,我得到對象對象。另外,如果我在以下結果中以chrome格式打印console.log(msg),也會

"<html>
    <head>
        <title>Unknown web method UpdateReportStream.<br>Parameter name: methodName</title>
        <style>
         body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;} 
         p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
         b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
         H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
         H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
         pre {font-family:"Lucida Console";font-size: .9em}
         .marker {font-weight: bold; color: black;text-decoration: none;}
         .version {color: gray;}
         .error {margin-bottom: 10px;}
         .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
        </style>
    </head>

    <body bgcolor="white">

            <span><H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1>

            <h2> <i>Unknown web method UpdateReportStream.<br>Parameter name: methodName</i> </h2></span>

            <font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">

            <b> Description: </b>An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

            <br><br>

            <b> Exception Details: </b>System.ArgumentException: Unknown web method UpdateReportStream.<br>Parameter name: methodName<br><br>

            <b>Source Error:</b> <br><br>

            <table width=100% bgcolor="#ffffcc">
               <tr>
                  <td>
                      <code>

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.</code>

                  </td>
               </tr>
            </table>

            <br>

            <b>Stack Trace:</b> <br><br>

            <table width=100% bgcolor="#ffffcc">
               <tr>
                  <td>
                      <code><pre>

[ArgumentException: Unknown web method UpdateReportStream.
Parameter name: methodName]
   System.Web.Script.Services.WebServiceData.GetMethodData(String methodName) +540418
   System.Web.Handlers.ScriptModule.OnPostAcquireRequestState(Object sender, EventArgs eventArgs) +213
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +148
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +75
</pre></code>

                  </td>
               </tr>
            </table>

            <br>

            <hr width=100% size=1 color=silver>

            <b>Version Information:</b>&nbsp;Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1022

            </font>

    </body>
</html>
<!-- 
[ArgumentException]: Unknown web method UpdateReportStream.
Parameter name: methodName
   at System.Web.Script.Services.WebServiceData.GetMethodData(String methodName)
   at System.Web.Handlers.ScriptModule.OnPostAcquireRequestState(Object sender, EventArgs eventArgs)
   at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
-->"

在ajax調用中,嘗試JSON.stringify輸入參數:

$.ajax({
     type: 'POST',
     url : 'Default.aspx/UpdateReportStream',
     data: JSON.stringify({category: selected }),
     contentType: 'application/json',
     success: function (msg) {

     },
     error: function (msg) {
     alert(msg);
     }
 });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM