0
我想使用AJAX調用CS頁面中的方法。如何使用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" %>
這裏從那裏我想用AJAX調用的SendMail方法(代碼我前面顯示你),這裏的CS文件是在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 = "[email protected]";
// 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方法。但面臨諸如「403禁用」和「500服務器未找到」等問題,無法使用AJAX調用sendmail方法。
調試器是否在ajax調用上觸發'SendMail' cs代碼?檢查一次。另外,在cs代碼中加入一個'try {...} catch(){...}'來檢查代碼中是否有任何smtp或一般異常。 –
沒有sendmail方法沒有得到調試。我把調試,但沒有得到調用。 –
你是怎麼解決這個問題的? –