Как мне вызвать функцию веб-сервиса C # на HTML-странице, используя Javascript?Я пытался использовать следующий код, но моя функция Javascript даже не взаимодействует с веб-сервисом.
Функция Javascript:
<script language="JavaScript" type="text/javascript">
var iCallID;
function getEmail()
{
iCallID=document.getElementById("email").value;
service.useService("http://localhost:1551/MyWebSite/MyWebService.asmx?wsdl", "GetDateTimeService");
alert(iCallID);
service.GetDateTimeService.callService("sendEmail",iCallID);
alert(event.result.value);
}
</script>
C # Webservices
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Net.Mail;
using System.Net;
/// <summary>
/// Summary description for MyWebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class MyWebService : System.Web.Services.WebService
{
public MyWebService()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string sendEmail(string EmailID)
{
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.Credentials = new NetworkCredential("some-email@gmail.com", "password");
client.EnableSsl = true;
MailMessage mailmsg = new MailMessage();
mailmsg.From = new MailAddress(EmailID);
mailmsg.To.Add("some-email@gmail.com");
mailmsg.Subject = "test";
mailmsg.Body = "test mail";
client.Send(mailmsg);
return "Email Sent Successfully";
}
}