[英]500 internal server error when submit data from AJAX in live server in asp.net
在使用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的網址,數據和其他內容。 因為我的想法是,當數據從AJAX獲取URL,數據和其他內容時,就會出現此錯誤。
////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;" /> ' + 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; }
試試這個。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.