>我已经在一个 asp.net 的Web应用程序中实现了一个弹出窗口,允许用户选择某些内容并生成电子邮件模板。完成所有必填字段后,用户通过单击按钮发送电子邮件。
此按钮调用一个 JavaScript 函数,我在其中收集所有用户输入数据,如 from、to、subject、body,并希望将此数据发送到我创建的 Web 服务,该服务将发送邮件。
我不想使用mailto,所以我选择了Web服务。
问题是我无法发送数据:
$.ajax({
type: 'POST',
url: "Services/MailService.asmx/SendMail",
dataType: "json",
contentType: 'application/json; charset=utf-8',
data: { 'loginName': "'" + loginName + "'", 'fromAddress': "'" + fromAddress + "'", 'toAddress': "'" + toAddress + "'", 'mailSubject': "'" + mailSubject + "'", 'mailBody': "'" + mailBody + "'"},
success: function ()
{
alert("The email was sent successfully!");
},
error: function(data)
{
alert("An error occurred while trying to send the email. " + data.responseText);
}
});
我知道我在数据属性中插入的内容是错误的。我尝试了以下方法(一次一个(:
data: JSON.stringify({ 'loginName': loginName, 'fromAddress': fromAddress, 'toAddress': toAddress, 'mailSubject': mailSubject, 'mailBody': mailBody }),
data: { 'loginName': loginName, 'fromAddress': fromAddress, 'toAddress': toAddress, 'mailSubject': mailSubject, 'mailBody': mailBody },
data: { loginName: loginName, fromAddress: fromAddress, toAddress: toAddress, mailSubject: mailSubject, mailBody: mailBody },
data: "{ 'loginName': '" + loginName + "', 'fromAddress': '" + fromAddress + "', 'toAddress': '" + toAddress + "', 'maliSubject': '" + mailSubject + "', 'mailBody': '" + mailBody + "' }",
所有这些选项都给了我以下错误:
An error occurred while trying to send the email. {"Message":"Invalid web service call, missing value for parameter: u0027mailBbodyu0027.","StackTrace":" at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters)rn at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)rn at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}
或
An error occurred while trying to send the email. {"Message":"Invalid JSON primitive: loginName.","StackTrace":" at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()rn at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)rn at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)rn at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)rn at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}
我不明白我做错了什么。
Web 服务如下所示:
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public void SendMail(string loginName, string fromAddress, string toAddress, string mailSubject, string mailBody)
{
MailMessage mailMsg = new MailMessage(fromAddress, toAddress);
mailMsg.Subject = mailSubject;
mailMsg.Body = mailBody;
string pathToCreate = "~/Upload/" + loginName + "/";
if (Directory.Exists(Server.MapPath(pathToCreate)))
{
if (Directory.GetFiles(Server.MapPath(pathToCreate)).Length > 0)
{
string[] attachedFiles = Directory.GetFiles(Server.MapPath(pathToCreate));
foreach (string a in attachedFiles)
{
Attachment data = new Attachment(a);
mailMsg.Attachments.Add(data);
}
}
}
SmtpClient smtp = new SmtpClient();
smtp.Send(mailMsg);
}
}
我该怎么做才能解决这个问题?
我能够通过以下方式成功发送数据:
data: '{loginName:' + JSON.stringify(loginName) + ', fromAddress:' + JSON.stringify(fromAddress) + ', toAddress:' + JSON.stringify(toAddress) + ', mailSubject:' + JSON.stringify(mailSubject) + ', mailBody:' + JSON.stringify(mailBody) + '}',
我不知道这是否是正确的方法,但它正在起作用。