Web方法不会调用,但不会出错

Web method wont call, but does not error

本文关键字:出错 调用 Web 方法      更新时间:2023-09-26

我正在努力让一个web方法在VB中工作。javascript被调用,但似乎从未调用过web方法。我在调试模式下运行,没有任何错误。

这是代码:

      <System.Web.Services.WebMethod()>
   Public Shared Sub PasteEvent(EventID As Integer, 
                                startDate As DateTime, 
                                endDate as DateTime, 
                                newStart As DateTime)
      ' work out the diff between start and end
      Dim difference As long = DateDiff(DateInterval.Minute,startDate,endDate)
      ' pasteStart  + minutes from the event start
      ' this is because we can only paste on the hour, but events may have started after the hour
      ' ie 10:15
      newStart.AddMinutes(startDate.Minute)
      ' new end = pastestart + diff
      Dim newEnd As DateTime = newStart.AddMinutes(convert.ToDouble(difference))
      ' call database 
      Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("Blueprint").ToString())

      Dim cmd As New SqlCommand
      cmd.CommandType = CommandType.StoredProcedure
      cmd.CommandText = "spOPSCopyEvent"
      cmd.Parameters.AddWithValue("@EventID", EventID)
      cmd.Parameters.AddWithValue("@StartDate", newStart)
      cmd.Parameters.AddWithValue("@EndDate", newEnd)
      cmd.Connection = conn
      conn.Open()
      cmd.ExecuteNonQuery()
      conn.Close()

   End sub

调用它的javascript:

 <script type="text/javascript" language="javascript">
      function eventCopy(eventID, start, end)
      {
         alert("copy");
         // grab the event id and store it in a hidden text box
         $("#ctl00_MainContent_hidCopyEventID").val(eventID);
         $("#ctl00_MainContent_hidCopyStart").val(start);
         $("#ctl00_MainContent_hidCopyEnd").val(end);
      }
      function eventPaste(eventStart)
      {
         alert("paste");
         alert(eventStart);
         // Call a web method, passing the eventID and the new start time
         var eventID = $("#ctl00_MainContent_hidCopyEventID").val;
         var startDate = $("#ctl00_MainContent_hidCopyStart").val;
         var endDate = $("#ctl00_MainContent_hidCopyEnd").val;
         PageMethods.PasteEvent(eventID, startDate, endDate, eventstart)
      }
   </script>

到目前为止,我有:

更新了主页中的脚本管理器,使其具有enablePageMethods="true"

尝试添加Imports System.Web.Services

将javascript移动到主体而不是头部

问题是您缺少异常处理机制看看你犯了什么错误。

  1. 在javascript和vb代码中放入try-and-catch并打印错误。

  2. 像Fidler一样使用嗅探器来查看您发送的内容。

  3. 尝试使用在web服务中打印跟踪消息Trace.Log,你可以在运行DebugView后看到它们,并查看你的下落

使用此行时:

var eventID = $("#ctl00_MainContent_hidCopyEventID").val; 

我应该放:

var eventID = $("#ctl00_MainContent_hidCopyEventID").val(); 

接受格雷戈里的回答是最有助于诊断这个问题的。