简体   繁体   English

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

[英]Call ASP.NET web service method from JavaScript

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("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);
}

and i have a html page. 而且我有一个html页面。 How can i call this function from my html page? 我如何从我的HTML页面调用此函数? (This function must send the message to email) (此功能必须将消息发送到电子邮件)

Use jQuery library. 使用jQuery库。 It makes ajax calls piece a cake. 它使ajax成为一个蛋糕。 Then follow these items: 然后按照以下项目操作:

  1. Add an HTML button to your page, so that people can start the ajax process by clicking it 在页面上添加一个HTML按钮,以便人们可以通过单击它来启动ajax进程
  2. Use jQuery to hook into the click event of that button (or span, or a div, or anything else) 使用jQuery钩住该按钮(或span或div或其他任何东西)的click事件
  3. Make sure you have ScriptService attribute on your web service (this attribute means that you can call your service from JavaScript) 确保您的Web服务上具有ScriptService属性(此属性意味着您可以从JavaScript调用服务)
  4. Send items to your web service method 将项目发送到您的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){} }); }); 

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. 只是请注意,您必须使用参数制作一个JSON对象,并且JSON属性的名称应与Web服务参数的名称匹配。 Also note that the return value of the web service would be available to you as rd object passed to success callback of ajax call. 还请注意,作为传递给ajax调用成功回调的rd对象,您可以使用Web服务的返回值。

您必须通过cs代码页传递三个参数,方法是像这样调用:

service.SendMail(name, email, message);

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

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

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属性添加到该类,以便可以从Javascript调用它,如以下示例所示:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM