从asp.net中的实时服务器中的AJAX提交数据时出现500内部服务器错误

时间:2017-08-06 10:04:36

标签: jquery asp.net ajax server

我在使用c#在asp.net上使用ajax提交数据时遇到错误。

错误是“500(内部服务器错误)”

这是我的代码

<script>
    $(function () {
        $(document).on("click", "#submit_mail", function (e) {
            if (validateform() == false) {
                e.preventDefault();
            }
            else {
                $.ajax({
                    type: "POST",
                    url: "contact.aspx/SendMail",

                    data: JSON.stringify({ name: $('#txt_name').val(), email: $('#txt_emailID').val(), subject: $('#txt_subject').val(), message: $('#txt_content').val() }),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function () {
                        alert("Email Send Successfully, we will contact you successfully...!!!");
                        location.reload();
                    },
                    failure: function () {
                        alert("There is some problem in server to contact with us....Please contact with contact number...!!!");
                    }
                });
            }
        });
    });
</script>

现在,这是代码。此代码在本地服务器中完美运行。在实时服务器中,此代码无效。

当我从开发人员工具调试这个ajax时,它表明它调用了ajax行,即 $.ajax({并结束)}; 在调试之间没有进行,所以从那个为什么在webserver方法中,它是服务器端代码,它将空数据并显示500内部服务器错误。

这是服务器端代码

 [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static void SendMail(string name, string email, string subject, string message)
    {
        //Thread.Sleep(10000);
        // Gmail Address from where you send the mail
        var fromAddress = "xxxxxxxxxxxxx@gmail.com";
        // any address where the email will be sending
        var toAddress = email.Trim();
        //Password of your gmail address
        const string fromPassword = "xxxxxxxxxxx";
        // Passing the values and make a email formate to display
        string sub = subject.Trim();
        string body = "From: " + name.Trim() + "\n";
        body += "Email: " + email.Trim() + "\n";
        body += "Subject: " + subject.Trim() + "\n";
        body += "Message: \n" + message.Trim() + "\n";
        // smtp settings
        var smtp = new System.Net.Mail.SmtpClient();
        {
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
            smtp.Timeout = 20000;
        }
        // Passing values to smtp object
        smtp.Send(fromAddress, toAddress, sub, body);
    }

所以,简而言之,我的问题是,为什么在AJAX方法调试中没有采用AJAX的url,数据和其他内容。因为我认为当数据从AJAX获取网址,数据和其他内容时会出现此错误。

1 个答案:

答案 0 :(得分:0)

////Send Email on button click
function SendEmail(url) {

    obj = {};
    obj.Name = $("#txtName").val();
    obj.Email = $("#txtEmail").val();
    obj.MobileNo = $("#txtMobileNo").val();

    obj.Subject = ($("#txtSubject").val() != '') ? $("#txtSubject").val() : '';
    obj.Message = ($("#txtMessage").val() != '') ? $("#txtMessage").val() : '';

    $.ajax({
        type: "POST",
        url: url,
        data: JSON.stringify(obj),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: function (response) {
            alert(response.d);
        }
    });

    function OnSuccess(response) {
        $.each(response.d, function () {

            var Msg;
            var Result;
            Msg = this['Text'];
            Result = this['Value'];

            if (Result > 0) {
                Clear();
                $("#AlertClass").attr("style", "display: block")
                $('#AlertClass').addClass('alert alert-success text-center');
                $('#lblResult').append('<span><img src="assets/images/Serve.png" style=" height: 40px;" /> &nbsp;&nbsp;' + Msg + '</span>');

            } else {
                $("#AlertClass").attr("style", "display: block")
                $('#AlertClass').addClass('alert alert-danger text-center');
                $('#lblResult').append('<span>"' + Msg + '"</span>');
            }
        });
    }
}
        [WebMethod]
        public static List<ListItem> SendEmail(string Name, string Email, string MobileNo, string Subject, string Message)
        {
          
            int Return = 0;
            try
            {
            }
            catch (Exception)
            {
                ReturnMessage.Add(new ListItem
                {
                    Value = "0",
                    Text = "There is an error try again after some time!"
                });
            }

            return ReturnMessage;
        }

尝试这个。