当在asp.net的实时服务器中从AJAX提交数据时发生500内部服务器错误
问题描述:
我在使用c#在asp.net中创建网站的实时服务器上从ajax提交数据时出现错误。当在asp.net的实时服务器中从AJAX提交数据时发生500内部服务器错误
的错误是 “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({
和这就是为什么在Web服务器的方法是服务器端的代码,它会之间的调试是不会结束它)};
等空数据并显示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 = "[email protected]";
// 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方法调试未服用的URL,数据和AJAX的其他内容。因为对于我的想法,当数据从AJAX获取网址,数据和其他内容时,这个错误即将到来。
答
////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;
}
试试这个。
+0
通过在本地服务器中尝试您的代码只显示500(内部服务器错误) –
show me c#code。 –
我已更新数据,请检查它。但对我的想法有一个Ajax问题,因为它不是内容的网址,数据和其他内容,并从该Web服务器方法为空。因此得到这个错误。 –
似乎url问题的URL我们不能找到方法你打电话你可以在这里粘贴你的控制台错误吗? – Aamir