我想使用AJAX调用CS页面中的方法。
这是我的设计代码:
<!-- Name -->
<input type="text" name="name" id="name" required="required" class="form" placeholder="Name" />
<!-- Email -->
<input type="email" name="mail" id="mail" required="required" class="form" placeholder="Email" />
<!-- Subject -->
<input type="text" name="subject" id="subject" required="required" class="form" placeholder="Subject" />
<!-- Message -->
<textarea name="message" id="message" class="form textarea" placeholder="Message"></textarea>
<!-- Send Button -->
<button type="button" id="submit" name="submit" class="form-btn semibold">Send Message</button>
这是ajax
$(document).on("click", "#submit", function (e) {
$.ajax({
type: "POST",
url: "OakscrollWebService.cs/SendMail",
dataType: "json",
data: JSON.stringify({ name: $('#name').val(), email: $('#mail').val(), subject: $('#subject').val(), message: $('#message').val() }),
contentType: "application/json; charset=utf-8",
success: function (data) {
alert(data.d);
},
failure: function (data) {
alert("something went wrong");
//console.log(msg);
}
});
});
现在,我添加了asmx页面(Web服务)。在那里我给了App_Code文件夹中的CS文件的引用调用,这里是代码。
<%@ WebService Language="C#" CodeBehind="~/App_Code/OakscrollWebService.cs" Class="OakscrollWebService" %>
这里是cs文件,我想使用ajax调用SendMail方法(我之前显示的代码),这里是cs文件中的方法代码
[WebMethod]
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 = "mextra03@gmail.com";
// any address where the email will be sending
var toAddress = email.Trim();
//Password of your gmail address
const string fromPassword = "*********";
// 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调用sendmail方法的内容。但面对的问题包括&#34; 403禁止&#34;和&#34; 500服务器未找到&#34;并且无法使用AJAX调用sendmail方法。
答案 0 :(得分:0)
您好我认为您需要更改您的网址以使用文件扩展名.asmx
而不是.cs
(将行url: "OakscrollWebService.cs/SendMail",
更改为url: "OakscrollWebService.asmx/SendMail",
),因为我在测试时我收到404错误意味着找不到页面。您还可以使用JSONLint创建一个变量来检查您的JSON是否正确。
$(document).on("click", "#submit", function (e) {
var data = '{"name":"' + $('#name').val() + '", "email":"' + $('#mail').val() + '", "subject":"' + $('#subject').val() + '", "message":"' + $('#message').val() + '"}'; //Optional to check your JSON
$.ajax({
type: "POST",
url: "OakscrollWebService.asmx/SendMail",
dataType: "json",
data: JSON.stringify({ name: $('#name').val(), email: $('#mail').val(), subject: $('#subject').val(), message: $('#message').val() }),
contentType: "application/json; charset=utf-8",
success: function (data) {
alert(data.d);
},
failure: function (data) {
alert("something went wrong");
//console.log(msg);
}
});
});
});
祝你好运!