从JavaScript调用ASP.NET web服务方法

从JavaScript调用ASP.NET web服务方法,javascript,asp.net,ajax,web-services,webmethod,Javascript,Asp.net,Ajax,Web Services,Webmethod,我有网络服务: [WebMethod] public void SendMail(string _name, string _email, string _message) { //Create Mail Message Object with content that you want to send with mail. MailMessage MyMailMessage = new MailMessage("gglebati@example.com", "gglebati@

我有网络服务:

[WebMethod]
public void SendMail(string _name, string _email, string _message)
{
    //Create Mail Message Object with content that you want to send with mail.
    MailMessage MyMailMessage = new MailMessage("gglebati@example.com", "gglebati@example.com", "This is the mail subject", "Just wanted to say Hello");

    MyMailMessage.IsBodyHtml = false;

    //Proper Authentication Details need to be passed when sending email from gmail
    NetworkCredential mailAuthentication = new NetworkCredential("myxxxxx@gmail.com", "xxxxxxxxx");

    //Smtp Mail server of Gmail is "smpt.gmail.com" and it uses port no. 587
    //For different server like yahoo this details changes and you can
    //get it from respective server.
   SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);

    //Enable SSL
    mailClient.EnableSsl = true;
    mailClient.UseDefaultCredentials = false;
    mailClient.Credentials = mailAuthentication;

    mailClient.Send(MyMailMessage);
}

我有一个html页面。如何从html页面调用此函数?(此功能必须将消息发送到电子邮件)

如果是aspx页面,则可以添加具有EnablePageMethods=“true”属性的ScriptManager,然后调用PageMethods.sendMail(名称、电子邮件、消息、onSuccess、onFail)

如果是aspx页面,您可以添加一个具有EnablePageMethods=“true”属性的ScriptManager,然后调用PageMethods.sendMail(名称、电子邮件、消息、onSuccess、onFail)

也许您想看看jQuery的功能。

也许您想看看jQuery的功能。

使用jQuery库。它使ajax调用变得轻而易举。然后按照以下项目操作:

  • 在页面中添加一个HTML按钮,这样人们就可以通过单击它来启动ajax过程
  • 使用jQuery钩住该按钮(或span、div或其他任何内容)的click事件
  • 确保您的web服务上有
    ScriptService
    属性(该属性意味着您可以从JavaScript调用您的服务)
  • 将项目发送到web服务方法

     $('#buttonId').click(function(){
         // Validating input
         $.ajax({
            type: 'POST',
            url: '/your-web-service-path.asmx/your-method-name',
            data: {} 
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            success: function(r){},
            error: function(e){}
         });
    });
    

  • 请注意,您必须使用参数生成JSON对象,JSON属性的名称应该与web服务参数的名称匹配。还要注意,web服务的返回值将作为
    r.d
    对象传递给ajax调用的成功回调。

    使用jQuery库。它使ajax调用变得轻而易举。然后按照以下项目操作:

  • 在页面中添加一个HTML按钮,这样人们就可以通过单击它来启动ajax过程
  • 使用jQuery钩住该按钮(或span、div或其他任何内容)的click事件
  • 确保您的web服务上有
    ScriptService
    属性(该属性意味着您可以从JavaScript调用您的服务)
  • 将项目发送到web服务方法

     $('#buttonId').click(function(){
         // Validating input
         $.ajax({
            type: 'POST',
            url: '/your-web-service-path.asmx/your-method-name',
            data: {} 
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            success: function(r){},
            error: function(e){}
         });
    });
    

  • 请注意,您必须使用参数生成JSON对象,JSON属性的名称应该与web服务参数的名称匹配。还请注意,web服务的返回值将作为传递给ajax调用成功回调的
    r.d
    对象提供给您。

    您需要做一些非常重要的事情:将ScriptService属性添加到类中,以便可以从Javascript调用它,如本例所示:

    [ScriptService]
    public class SimpleWebService : System.Web.Services.WebService 
    {
        // ...
    }
    

    您需要做一些非常重要的事情:将ScriptService属性添加到类中,以便可以从Javascript调用它,如本例所示:

    [ScriptService]
    public class SimpleWebService : System.Web.Services.WebService 
    {
        // ...
    }
    

    您必须通过如下方式调用,从cs代码页传递三个参数

    service.SendMail(name, email, message);
    

    您必须通过如下方式调用,从cs代码页传递三个参数

    service.SendMail(name, email, message);
    

    可能重复的您是否愿意添加jQuery库?@失望先生-我不认为这是该页面的重复。可能重复的您是否愿意添加jQuery库?@失望先生-我不认为这是该页面的重复。这是带Javascript的simle html页面这是带Javascript的simle html页面我怎么能发布参数:(string\u name,string\u email,string\u message)>?创建此对象:
    {string\u name:'value1',string\u email:'value2',string\u message:'value3'}
    @\u glebati:查看数据的位置:{}?你可以输入:数据:{“你的消息”:“你好,世界”,“你的名字”:“约翰·史密斯”,“你的电子邮件”:john@example.com' }. 重要的是,web服务中的参数名称与javascript端数据参数上的名称匹配,并且数据参数是有效的JSON格式。我如何发布参数:(string\u name,string\u email,string\u message)>?创建此对象:
    {string\u name:'value1',string\u email:'value2',string\u message:'value3'}
    @\u glebati:查看数据:{}的位置?您可以放置:数据:{u message':'hello world','u name':'john smith','u email':'john@example.com' }。重要的是,web服务中的参数名称与javascript端的数据参数名称相匹配,并且数据参数是有效的JSON格式。+1因为这是一个有效的答案,但您应该提供更多的细节。类似于这样说“您可能想阅读.Net Web服务文档@了解更多信息。+1因为这是一个有效的答案,但您应该提供更多的细节。类似于说“您可能想阅读.Net Web服务文档@了解更多信息。”。