Ajax Call to send Email Not working。

时间:2017-02-24 19:17:23

标签: javascript jquery ajax asp.net-mvc

我是JQuery的新手。伙计们可以告诉我我的代码有什么问题我想通过ajax发送电子邮件然后在div中填充返回的字符串。我试图将数据从我的视图发送到我的控制器使用数据,然后发回一个字符串添加到Dom。我怎样才能做到这一点。感谢您的时间和精力。欢呼声。

我的观点

<!-- Contact form -->
                <form id="contact-form" name="contact-form" method="POST" data-name="Contact Form">

                    <div class="row">

                        <!-- Full name -->
                        <div class="col-xs-12 col-sm-6 col-lg-6">
                            <div class="form-group">
                                <input type="text" id="name" class="form form-control" placeholder="Write your name" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Write your name'" name="name" data-name="Name" required>
                            </div>
                        </div>

                        <!-- E-mail -->
                        <div class="col-xs-12 col-sm-6 col-lg-6">
                            <div class="form-group">
                                <input type="email" id="email" class="form form-control" placeholder="Write your email address" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Write your email address'" name="email" data-name="Email Address" required>
                            </div>
                        </div>

                        <!-- Subject -->
                        <div class="col-xs-12 col-sm-12 col-lg-12">
                            <div class="form-group">
                                <input type="text" id="subject" class="form form-control" placeholder="Write the subject" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Write the subject'" name="subject" data-name="Subject">
                            </div>
                        </div>

                        <!-- Message -->
                        <div class="col-xs-12 col-sm-12 col-lg-12 no-padding">
                            <div class="form-group">
                                <textarea id="text-area" class="form textarea form-control" placeholder="Your message here... 20 characters Min." onfocus="this.placeholder = ''" onblur="this.placeholder = 'Your message here... 20 characters Min.'" name="message" data-name="Text Area" required></textarea>
                            </div>
                        </div>

                    </div>

                    <!-- Button submit -->
                    <button type="submit" id="valid-form" class="btn btn-color">Send my Message</button>

                </form>
                <!-- /. Contact form -->

                <div id="block-answer">
                    <div id="answer"></div>
                </div>

Body底部的脚本

$('#valid-form').click(function () {
        var name = $('#name').val();
        var email = $('#email').val();
        var subject = $('#subject').val();
        var message = $('#message').val();
        $.ajax({
            url: '/Home/SendMessage',
            data: { 'name': name, 'email': email, 'subject': subject, 'message': message },
            type: "post",
            cache: false,
            success: function (status) {
                $('#answer').text(savingStatus);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                $('#answer').text("Error encountered while saving the message.");
            }
        });
    });

我的控制器

[HttpPost]
    public string SendMessage(string name, string email, string subject, string message)
    {
        var savingStatus = "Success";
        return savingStatus;
    }

1 个答案:

答案 0 :(得分:0)

您使用字符串作为字段名称,请用此替换数据。

data: { name: name, email: email, subject: subject, message: message },