0

This is probably something simple, but I can't find what is wrong to give me this problem.

I have a form in a HTML page that calls, on click, a method in a controller via Ajax. It works perfectly in my development machine, but when I put it in production environment the call never gets to the controller. Anyone have any ideas why? I've seen many questions like this, most of them saying it's most likely a problem with the action path, but I've tried a lot of diferent ways to call it (with razor html helpers, passing the absolute path, etc) and got nothing.

The form (simple):

<form action="/Home/SendMail/" id="contactForm" type="post">
                                        <input type="text" name="name" id="name">
                                        <input type="email" name="email" id="email">
                                        <input type="text" name="subject" id="subject">
                                        <textarea name="message" id="message"></textarea>
                                    <input type="submit" value="Enviar Mensagem">
                            </form>

The ajax call:

$("#contactForm").validate({
            submitHandler: function(form) {

                var submitButton = $(this.submitButton);
                submitButton.button("loading");
                var url = $("#contactForm").attr("action");
                $.ajax({
                    type: "POST",
                    url: url,
                    data: {
                        "name": $("#contactForm #name").val(),
                        "email": $("#contactForm #email").val(),
                        "subject": $("#contactForm #subject").val(),
                        "message": $("#contactForm #message").val()
                    },
                    dataType: "json",
                    success: function (data) {
                        if (data.response == "success") {

                            $("#contactSuccess").removeClass("hidden");
                            $("#contactError").addClass("hidden");

                            $("#contactForm #name, #contactForm #email, #contactForm #subject, #contactForm #message")
                                .val("")
                                .blur()
                                .closest(".control-group")
                                .removeClass("success")
                                .removeClass("error");

                            if(($("#contactSuccess").position().top - 80) < $(window).scrollTop()){
                                $("html, body").animate({
                                     scrollTop: $("#contactSuccess").offset().top - 80
                                }, 300);
                            }

                        } else {

                            $("#contactError").removeClass("hidden");
                            $("#contactSuccess").addClass("hidden");

                            if(($("#contactError").position().top - 80) < $(window).scrollTop()){
                                $("html, body").animate({
                                     scrollTop: $("#contactError").offset().top - 80
                                }, 300);
                            }

                        }
                    },

The controller method:

public class HomeController : Controller
    {
        [HttpPost]
        public JsonResult SendMail(string name, string email, string subject, string message)
        {
            try
            {
                string emailTo = "[email protected]";

                MailMessage Email = new MailMessage();
                Email.From = new MailAddress("[email protected]");
                string[] emailsDestinarios = emailTo.Split(';');
                foreach (string emailDestinatario in emailsDestinarios)
                {
                    Email.To.Add(new MailAddress(emailDestinatario));
                }
                Email.Subject = subject;
                Email.IsBodyHtml = true;
                Email.Body = DateTime.Now.ToString() + "<br />" + name + "<br />" + email + "<br /><br /><b>" + subject + "</b><br /><br />" + message;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.example.com";
                smtp.Port = 587;
                smtp.EnableSsl = true;
                smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "mypassword");               
                smtp.Send(Email);
                return Json(new { response = "success" });
            }
            catch (Exception ex)
            {                
                return Json(new { response = ex.Message });
            }
        }
    }

It works fine in development, but not in production environment.

2
  • 1
    Pop open your console, whats the network request status?
    – tymeJV
    Jan 6, 2014 at 19:12
  • It's giving me a 404 (Not Found) Jan 6, 2014 at 19:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.