【发布时间】:2017-08-06 10:04:35
【问题描述】:
在实时服务器上从 ajax 提交数据时出现错误,该网站是用 c# 在 asp.net 中创建的。
错误是“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 获取 url、数据和其他内容时,就会出现这个错误。
【问题讨论】:
-
显示 c# 代码。
-
我已更新数据,请查看。但对于我的想法,存在 ajax 问题,因为它不是内容 url、数据和其他内容,因此 webserver 方法为空。因此出现此错误。
-
似乎是 url 问题 url 你的输入找不到你调用的方法你能把你的控制台错误粘贴在这里吗?
标签: jquery asp.net ajax server