javascript - 如何在 ASP.NET 中异步调用 Web 方法(后面的 SharePoint)

标签 javascript c# asp.net ajax sharepoint-2013

<分区>

调用此异步 Web 方法时,出现 500 错误。还有其他方法可以异步调用 Web 方法吗?

堆栈跟踪:

Server Error in '/' Application.

Unknown web method SendMessage. Parameter name: methodName
Description: 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.

Exception Details: System.ArgumentException: Unknown web method SendMessage. Parameter name: methodName

Source Error:

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.

Stack Trace:

[ArgumentException: Unknown web method SendMessage. Parameter name: methodName]
System.Web.Handlers.ScriptModule.OnPostAcquireRequestState(Object sender, EventArgs eventArgs) +728343
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +92 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +165

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34248

ASPX 代码隐藏:

namespace Any.Contact
{
    using System.Threading.Tasks;
    using System.Web.Services;

    public partial class ContactFormPage : PublishingLayoutPage
    {    
        [WebMethod]
        public static async Task<bool> SendMessage(string topic, string message)
        {
            return await SendEmail(topic, message);
        }
    }
}

JavaScript:

$.ajax({
  type: "GET",
  url: window.location.href + "/SendMessage",
  data: {
    "topic": jQuery('.ddl-topic').val(),
    "message": jQuery('.tb-message').val()
  },
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  async: true,
  cache: false,
  success: function() {
    $('.alert.alert-success').toggleClass('hidden', 'show');
  },
  error: function() {
    $('.alert.alert-danger').toggleClass('hidden', 'show');
  }
});

最佳答案

不用Task,JS会异步调用。它可以是返回 bool 的常规方法。 尝试:

namespace Any.Contact
{
    using System.Threading.Tasks;
    using System.Web.Services;

    public partial class ContactFormPage : PublishingLayoutPage
    {    
        [WebMethod]
        public static bool SendMessage(string topic, string message)
        {
            return SendEmail(topic, message);
        }
    }
}

关于javascript - 如何在 ASP.NET 中异步调用 Web 方法(后面的 SharePoint),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31291479/

相关文章:

c# - 如果线程无论如何都被阻塞,异步 IO 有什么意义(参见示例)

asp.net - 基本的 Mono 安装不起作用

c# - 当用户从 DropDownList 中选择不同选项时如何更改内容

javascript - 多平台 HTML5 游戏 DOM+CSS vs CANVAS vs 两者

javascript - Gatsby 节点错误 : "Must Provide Source" error being thrown

Javascript单例,如何更新这个变量?

c# - 循环集合中除新行之外的每一行。

c# - MySQL 错误 : Column 'accountStatus' in field list is ambiguous

c# - 单选按钮列表?当我选择一个单选按钮时,我想通过再次单击选定的单选按钮来取消选择

javascript - 如何在 Angular JS 中的自定义标签中访问 HTML?