簡體   English   中英

使用Ajax從javascript調用Web Service .asmx

[英]Calling Web Service .asmx from javascript using ajax

我已經在asp.net網絡應用程序中實現了一個彈出窗口,該彈出窗口允許用戶選擇某些內容並生成電子郵件模板。 完成所有必填字段后,用戶單擊按鈕即可發送電子郵件。

此按鈕調用javascript函數,在其中我收集所有用戶輸入數據,例如從,到,主題,正文,並希望將此數據發送到我創建的Web服務中,該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屬性中插入的內容是錯誤的。 我嘗試了以下操作(一次):

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: \u0027mailBbody\u0027.","StackTrace":"   at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters)\r\n   at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n   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()\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n   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) + '}',

我不知道這是否是正確的方法,但它正在起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM