【问题标题】:Call ASP.NET web service method from JavaScript从 JavaScript 调用 ASP.NET Web 服务方法
【发布时间】:2011-09-01 12:49:04
【问题描述】:

我有网络服务:

[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 页面调用此函数? (此功能必须将消息发送到电子邮件)

【问题讨论】:

标签: javascript asp.net ajax web-services webmethod


【解决方案1】:

使用 jQuery 库。它使 ajax 调用小菜一碟。然后按照以下项目:

  1. 向您的页面添加一个 HTML 按钮,以便人们可以通过单击它来启动 ajax 进程
  2. 使用 jQuery 挂钩该按钮(或 span、div 或其他任何东西)的单击事件
  3. 确保您的 Web 服务上有 ScriptService 属性(此属性意味着您可以从 JavaScript 调用您的服务)
  4. 将项目发送到您的网络服务方法

     $('#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 调用的成功回调。

【讨论】:

  • 如何发布参数:(string_name, string_email, string_message) >?
  • 创建这个对象:{string_name: 'value1', string_email: 'value2', string_message: 'value3'}.
  • @_glebati:查看哪里的数据:{}?你可以输入:data: {'_message':'hello world','_name':'john smith','_email':'john@example.com' }。重要的是您在 Web 服务中的参数名称与 javascript 端的数据参数名称匹配,并且数据参数是有效的 JSON 格式。
【解决方案2】:

如果是aspx页面,可以添加一个EnablePageMethods="true"属性的ScriptManager,然后调用PageMethods.sendMail(name, email, message, onSuccess, onFail);

【讨论】:

  • 这是一个带有 javascript 的简单 html 页面
【解决方案3】:

也许您想看看 jQuery 的 ajax 功能。

【讨论】:

【解决方案4】:

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

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

http://msdn.microsoft.com/en-us/library/system.web.script.services.scriptserviceattribute.aspx

【讨论】:

    【解决方案5】:

    你必须从cs代码页面传递三个参数,通过这样的调用,

    service.SendMail(name, email, message);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多