【问题标题】:Using jQuery and Web Services to send email使用 jQuery 和 Web 服务发送电子邮件
【发布时间】:2018-05-19 10:15:41
【问题描述】:

我正在尝试将发送电子邮件功能添加到纯 html 站点。我在 VS 2017 中创建了一个 ASP.Net 项目并将 html 文件添加到其中。在有“联系我们”表单的页面上,我添加了 jQuery 以使用 Web 服务发送电子邮件。我不断收到错误,不知道如何调试它。我在 Web 服务中单独测试了该方法,它工作正常,它发送电子邮件,这意味着我的 jQuery 中一定有问题。我需要这方面的帮助,看看是否有人能发现我缺少的东西。

当我运行它时,我得到状态码 500 返回。

<section id="contact" class="bg-light">
    <div class="container">
        <div class="row">
            <div class="col-sm-12 ml-sm-auto col-md-10 pt-4">
                <h1>Contact us</h1>

                <form id="sendmail" method="POST" action="">
                    <div class="form-group">
                        <input type="email" class="form-control" id="inputEmail" aria-describedby="emailHelp" placeholder="Email Address">
                    </div>
                    <div class="form-group">
                        <input type="email" class="form-control" id="inputName" aria-describedby="nameHelp" placeholder="First and Last Name">
                    </div>
                    <div class="form-group">
                        <input type="email" class="form-control" id="inputPhone" aria-describedby="phoneHelp" placeholder="Contact Number">
                    </div>
                    <div class="form-group">
                        <label for="inputMsg">Feedback/Questions</label>
                        <textarea class="form-control" id="inputMsg" rows="3"></textarea>
                    </div>
                    <button type="submit" id="btnSubmit" class="btn btn-primary">Submit</button>
                </form>
            </div>
        </div>
    </div>
</section>

<script type="text/javascript">
    $("#btnSubmit").click(function () {
        debugger
        $("#sendmail").validate();

        if ($("#sendmail").valid()) {
            $.ajax({
                type: "POST",
                url: "/services/easg.asmx/SendEmail",
                cache: false,
                contentType: "application/json; charset=utf-8",
                data: "{ 'Msg': '" + $("#inputMsg").val() + "', " + "'To': 'someone@somewhere.com'," + "'From': '" + $("#inputEmail").val() + "'," + "'Name': '" + $("#inputName").val() + "'," + "'Subject': 'Guide Feedback'," + "'Phone': '" + $("#inputPhone").val() + "'" + "}",
                dataType: "json",
                complete: function (transport) {
                    alert(transport.status);
                    if (transport.status == 200) {
                        alert('Your message was sent; thank you for your feedback');
                    }
                    else {
                        alert("Failed to send feedback; please try again later. Status Code: " + transport.status);
                    }
                }
            });
        }
        //so that the page doesn't post back
        return false;
    });
</script>

网络服务:

namespace EAStyleGuide
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]

    // 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 easg : System.Web.Services.WebService
    {

        [WebMethod]
        [ScriptMethod(UseHttpGet = true)]

        private void SendEmail(string Msg, string To, string From, string Name, string Subject, string Phone)
        {
            using (var message = new MailMessage())
            {
                message.From = new MailAddress(From);
                message.To.Add(new MailAddress(To));
                message.Subject = Subject;
                message.Body = GetMailBody(From, Msg, Phone, Name);
                message.Priority = MailPriority.Normal;
                message.IsBodyHtml = false;

                SmtpClient sc = new SmtpClient("mailrelay.blah.com");
                sc.Credentials = new NetworkCredential();

            try
            {
                sc.Send(message);
            }
            catch (SmtpFailedRecipientsException FRE)
            {
            }
            catch (SmtpException smtpEx)
            {
            }
            catch (Exception generalEx)
            {
            }
        }
        return;
    }

项目根目录下有一个“Services”文件夹,里面有easg.asmx。

【问题讨论】:

  • A 500 错误意味着服务器出现故障。服务器的实际响应是什么?错误信息是什么?您发送到服务器的数据有效负载的结果值是多少?它是有效的 JSON 吗?当你调试服务器端代码时,它具体在哪里失败了?
  • 这就是问题大卫,我在 Web 服务的“SendEmal”方法中设置了一个断点,但我没有点击它。要么 jQuery 代码不好并且它不调用 Web 服务,要么只是在 Web 服务的方法中添加断点不足以在断点处停止。但是,当我将该方法放在一个类中并使用一些常量变量调用它时,它会发送电子邮件,所以我认为我的 SendEmail 方法是可以的。在 VS 中调试时如何检查服务器的实际响应?
  • 检查浏览器的调试工具。在那里您可以观察到页面中涉及的 HTTP 请求/响应,包括 AJAX 调用。你会想看看服务器的实际响应是什么。
  • 我截获了“transport”变量,它的respnseText是“Unknown web method SendEmail.Parameter name: methodName”
  • 那么它没有找到开始的网络方法。 UseHttpGet = true 是否将该网络方法限制为 GET 请求?因为您正在发送 POST 请求。

标签: c# jquery asp.net web-services


【解决方案1】:

由于对上述问题的 cmets 进行了一些调试...

respnseText 是“未知的网络方法 SendEmail。参数名称:methodName”

这意味着框架根本无法根据发出的请求找到 Web 方法。仔细观察,web 方法似乎仅限于 GET 请求:

[ScriptMethod(UseHttpGet = true)]

但是,您正在发出 POST 请求:

type: "POST",

您需要更改其中一个。由你决定。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多