从 JavaScript 调用 ASP.NET Web 服务方法
我有网络服务:
[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 jQuery 库。它使 ajax 调用变得小菜一碟。然后执行以下操作:
ScriptService
属性(此属性意味着您可以从 JavaScript 调用您的服务)将项目发送到您的 Web 服务方法
请注意,您必须根据参数创建一个 JSON 对象,并且 JSON 属性的名称应与 Web 服务参数的名称匹配。另请注意,Web 服务的返回值将作为传递给 ajax 调用的成功回调的 rd 对象提供给您。
Use jQuery library. It makes ajax calls piece a cake. Then follow these items:
ScriptService
attribute on your web service (this attribute means that you can call your service from JavaScript)Send items to your web service method
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.如果是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);
也许您想了解一下 jQuery 的 ajax 功能。
Maybe you want to take a look at jQuery's ajax capabilities.
您需要做一些非常重要的事情:向类添加 ScriptService 属性,以便可以从 Javascript 调用它,如下例所示:
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:
http://msdn.microsoft.com/en-us/library/system.web.script.services.scriptserviceattribute.aspx
您必须从 cs 代码页传递三个参数,通过这样调用,
You have to pass the three parameters from the cs code page, by calling like this way,