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

发布于 2024-12-02 14:41:56 字数 1443 浏览 0 评论 0原文

我有网络服务:

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

I have the web service:

[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("[email protected]", "[email protected]", "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("[email protected]", "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);
}

and i have a html page. How can i call this function from my html page? (This function must send the message to email)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小女人ら 2024-12-09 14:41:56

使用 jQuery 库。它使 ajax 调用变得小菜一碟。然后执行以下操作:

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

     $('#buttonId').click(function(){
         // 验证输入
         $.ajax({
            类型:'发布',
            url: '/your-web-service-path.asmx/your-method-name',
            数据: {} 
            数据类型:'json',
            内容类型:'应用程序/json;字符集=utf-8',
            成功:函数(r){},
            错误:函数(e){}
         });
    });
    

请注意,您必须根据参数创建一个 JSON 对象,并且 JSON 属性的名称应与 Web 服务参数的名称匹配。另请注意,Web 服务的返回值将作为传递给 ajax 调用的成功回调的 rd 对象提供给您。

Use jQuery library. It makes ajax calls piece a cake. Then follow these items:

  1. Add an HTML button to your page, so that people can start the ajax process by clicking it
  2. Use jQuery to hook into the click event of that button (or span, or a div, or anything else)
  3. Make sure you have ScriptService attribute on your web service (this attribute means that you can call your service from JavaScript)
  4. Send items to your web service method

     $('#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){}
         });
    });
    

Just note that you have to make a JSON object out of your parameters and the name of the JSON properties should match the name of the web service parameters. Also note that the return value of the web service would be available to you as r.d object passed to success callback of ajax call.

生生不灭 2024-12-09 14:41:56

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

If it's an aspx page, you can add a ScriptManager with EnablePageMethods="true"attribute, and then call PageMethods.sendMail(name, email, message, onSuccess, onFail);

千里故人稀 2024-12-09 14:41:56

也许您想了解一下 jQuery 的 ajax 功能。

Maybe you want to take a look at jQuery's ajax capabilities.

孤千羽 2024-12-09 14:41:56

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

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

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

You need to do something very important: Add the ScriptService attribute the class so that it can be called from a Javascript as in this example:

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

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

兮子 2024-12-09 14:41:56

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

service.SendMail(name, email, message);

You have to pass the three parameters from the cs code page, by calling like this way,

service.SendMail(name, email, message);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文