使用 jquery 在 ajax 的回调中调用 ajax
Calling ajax in callback of ajax using jquery
假设第一个 Ajax 调用会成功(是的,它会成功)。
我在回调中调用 ajax,如下所示
function OnSuccess(data) {
$('.modal-footer').show();
if (data.d[4].toLowerCase().indexOf("success") >= 0) {
$('#resetMsg').html('Password has been sent on ' + data.d[3]);
$.ajax({
type: "GET",
url: "frmLogin.aspx/SendMail",
data: '{"mailTo":"' + data.d[3] + '","reqName":"' + data.d[0] + '","username":"' + $('#txtUser').val() + '","password":"' + data.d[2] + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json"
});
}
else {
$('#resetMsg').html(data.d[4]);
}
}
我的 WebMethod
是
[WebMethod]
static void SendMail(string mailTo, string reqName, string username, string password)
{
CommonClient commonSvc = new CommonClient();
bool mailStatus = false;
if (mailTo != string.Empty)
{
bool isValid = CommonUtility.isValidEmail(mailTo);
if (!isValid)
{
mailStatus = false;
}
}
string From = ConfigurationManager.AppSettings["FromEmailID"];
string Subject = "LEAP - password reset";
StringBuilder body = new StringBuilder();
body.Append("Password reset successfully for " + reqName +"<br>");
body.Append("Username: " + username + "<br>");
body.Append("Password: " + password + "<br>");
string ErrorMsg = "";
try
{
MailAddress from = new MailAddress(From, "HDFCERGO");
MailMessage Mail = new MailMessage();
Mail.To.Add(mailTo);
Mail.From = from;
Mail.Subject = Subject;
Mail.Body = body.ToString();
if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["BCCEmailID"].ToString()))
{
Mail.Bcc.Add(ConfigurationManager.AppSettings["BCCEmailID"].ToString());
}
Mail.IsBodyHtml = true;
SmtpClient smtpMailObj = new SmtpClient();
smtpMailObj.Send(Mail);
Mail.Dispose();
mailStatus = true;
}
catch (Exception Ex)
{
ErrorMsg = Ex.Message;
mailStatus = false;
}
finally
{
LogMailSentDetails(body.ToString(), Subject, mailTo, From, mailStatus == true ? "Success" : "Failed", ErrorMsg);
}
但它永远不会被调用.. :( 是因为我没有正确传递参数吗?请帮助我 :(
WebMethod
属性需要与 public 方法一起使用:
How to: Use the WebMethod Attribute
改为:
[WebMethod]
public static void SendMail(string mailTo, string reqName, string username, string password)
{
//rest of code
您还需要使用 post
方法来调用该方法,因为这是默认行为,而不是 get
方法。
假设第一个 Ajax 调用会成功(是的,它会成功)。
我在回调中调用 ajax,如下所示
function OnSuccess(data) {
$('.modal-footer').show();
if (data.d[4].toLowerCase().indexOf("success") >= 0) {
$('#resetMsg').html('Password has been sent on ' + data.d[3]);
$.ajax({
type: "GET",
url: "frmLogin.aspx/SendMail",
data: '{"mailTo":"' + data.d[3] + '","reqName":"' + data.d[0] + '","username":"' + $('#txtUser').val() + '","password":"' + data.d[2] + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json"
});
}
else {
$('#resetMsg').html(data.d[4]);
}
}
我的 WebMethod
是
[WebMethod]
static void SendMail(string mailTo, string reqName, string username, string password)
{
CommonClient commonSvc = new CommonClient();
bool mailStatus = false;
if (mailTo != string.Empty)
{
bool isValid = CommonUtility.isValidEmail(mailTo);
if (!isValid)
{
mailStatus = false;
}
}
string From = ConfigurationManager.AppSettings["FromEmailID"];
string Subject = "LEAP - password reset";
StringBuilder body = new StringBuilder();
body.Append("Password reset successfully for " + reqName +"<br>");
body.Append("Username: " + username + "<br>");
body.Append("Password: " + password + "<br>");
string ErrorMsg = "";
try
{
MailAddress from = new MailAddress(From, "HDFCERGO");
MailMessage Mail = new MailMessage();
Mail.To.Add(mailTo);
Mail.From = from;
Mail.Subject = Subject;
Mail.Body = body.ToString();
if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["BCCEmailID"].ToString()))
{
Mail.Bcc.Add(ConfigurationManager.AppSettings["BCCEmailID"].ToString());
}
Mail.IsBodyHtml = true;
SmtpClient smtpMailObj = new SmtpClient();
smtpMailObj.Send(Mail);
Mail.Dispose();
mailStatus = true;
}
catch (Exception Ex)
{
ErrorMsg = Ex.Message;
mailStatus = false;
}
finally
{
LogMailSentDetails(body.ToString(), Subject, mailTo, From, mailStatus == true ? "Success" : "Failed", ErrorMsg);
}
但它永远不会被调用.. :( 是因为我没有正确传递参数吗?请帮助我 :(
WebMethod
属性需要与 public 方法一起使用:
How to: Use the WebMethod Attribute
改为:
[WebMethod]
public static void SendMail(string mailTo, string reqName, string username, string password)
{
//rest of code
您还需要使用 post
方法来调用该方法,因为这是默认行为,而不是 get
方法。