【发布时间】:2011-12-20 12:42:46
【问题描述】:
如何使用 Javascript 在 HTML 页面中调用 C# webservice 函数?我一直在尝试使用以下代码,但我的 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# 网络服务
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";
}
}
【问题讨论】:
-
您采取了哪些步骤来调试问题?
-
在 VS 2010 中做...通过放置警报弹出框,我正在逐步检查它..
-
有什么方法可以准确地测试出哪个部分失败了?例如,也许您可以让 Web 服务写入文件或标准输出,以便您知道它是否正常工作但不发送电子邮件。
-
不,我没有使用 jquery...我只是在努力调用 web 服务方法。是的,我的 web 服务工作正常...
-
@puneeth - 仅供参考,整个连载都投反对票?是的 - StackOverflow 有一个系统可以检测到这一点,并与这样做的人打交道。
标签: javascript html c#-4.0