从 JavaScript 调用 ASP.NET Web 服务方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7270532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call ASP.NET web service method from JavaScript
提问by GLeBaTi
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)
我有一个 html 页面。我如何从我的 html 页面调用这个函数?(此功能必须将消息发送到电子邮件)
回答by Saeed Neamati
Use jQuery library. It makes ajax calls piece a cake. Then follow these items:
使用 jQuery 库。它使 ajax 调用变得轻而易举。然后按照这些项目:
- Add an HTML button to your page, so that people can start the ajax process by clicking it
- Use jQuery to hook into the click event of that button (or span, or a div, or anything else)
- Make sure you have
ScriptService
attribute on your web service (this attribute means that you can call your service from JavaScript) 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){} }); });
- 在您的页面中添加一个 HTML 按钮,以便人们可以通过单击它来启动 ajax 进程
- 使用 jQuery 挂钩该按钮的点击事件(或 span、div 或其他任何东西)
- 确保您
ScriptService
的 Web 服务具有属性(此属性意味着您可以从 JavaScript 调用您的服务) 将项目发送到您的网络服务方法
$('#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.
请注意,您必须从参数中创建一个 JSON 对象,并且 JSON 属性的名称应与 Web 服务参数的名称匹配。另请注意,Web 服务的返回值将作为r.d
传递给 ajax 调用的成功回调的对象提供给您。
回答by Slavcho
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);
如果是aspx页面,可以添加一个带有EnablePageMethods="true"属性的ScriptManager,然后调用PageMethods.sendMail(name, email, message, onSuccess, onFail);
回答by Fabio Milheiro
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/en-us/library/system.web.script.services.scriptserviceattribute.aspx
回答by srinivas jilla
You have to pass the three parameters from the cs code page, by calling like this way,
你必须从 cs 代码页传递三个参数,通过这样调用,
service.SendMail(name, email, message);