Search code examples
c#jqueryasp.netajaxwebmethod

Calling ajax in callback of ajax using jquery


Assuming first Ajax call will succeed (yes, it is succeed).

I am calling ajax in that callback like below

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]);
                    }
                }

My WebMethod is

[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);
        }

but it never get called.. :( is it because I am not passing parameters properly? please help me with that :(


Solution

  • The WebMethod attribute needs to be used with a public method:

    How to: Use the WebMethod Attribute

    Change it to:

    [WebMethod]
    public static void SendMail(string mailTo, string reqName, string username, string password)
    {
     //rest of code
    

    You also need to use a post method to call the method as this is the default behaviour rather than a get method.