Hardik Gondalia
Hardik Gondalia

Reputation: 3717

JQuery AJAX in Master Page using asmx in ASP.NET

I am calling webservice for checkin Login in master page using jquery Ajax. JavaScript:

function Login() {
    var UserName = $("[id*=tbUserName]").val();
    var PassWord = $("[id*=tbPassword]").val();
    $.ajax({
        type: "POST",
        url: '<%= ResolveUrl("~/WebServiceLogin.asmx/LoginUser") %>',
        data: "{'username':'" + UserName + "','password':'" + PassWord + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var j = jQuery.parseJSON(response.d);
            if (j["msg"] == "success")
                window.location = 'UserDashHome.aspx';
            else {
               window.location = 'Login.aspx';
            }
        },
        error: function (response) {
            alert("error:" + response.data + response.d);
        }
    });
}

ASMX Code:

[ScriptMethod]
    [WebMethod(EnableSession = true)]    
    public string LoginUser(String username,String password)
    {
        try
        {
            MobileStoreEntities mse = new MobileStoreEntities();
            UserMast um = new UserMast();

            if (mse.UserMasts.Where(user => user.Email == username && user.Password == password).Count() > 0)
            {
                if (mse.UserMasts.Where(user => user.Email == username && user.Password == password && user.Status==true).Count() > 0)
                {
                    EncryptDecrypt ed=new EncryptDecrypt();
                    HttpContext.Current.Session["LoginUserName"] = ed.Encrypt(username);
                    var userObj = mse.UserMasts.First(a => a.Email == username);
                    int id = userObj.UserID;
                    HttpContext.Current.Session["LoginID"] = ed.Encrypt(id.ToString());    

                    return "{\"msg\":\"success\"}";
                }
                else
                {
                    return "{\"msg\":\"Please make sure you make complete varification process if yes then contact admin.!\"}";
                }
            }

            if (mse.UserMasts.Where(user => user.UserName == username && user.Password == password).Count() > 0)
            {
                if (mse.UserMasts.Where(user => user.UserName == username && user.Password == password && user.Status==true).Count() > 0)
                {
                    EncryptDecrypt ed = new EncryptDecrypt();
                    HttpContext.Current.Session["LoginUserName"] = ed.Encrypt(username);
                    var userObj = mse.UserMasts.First(a => a.UserName == username);
                    int id = userObj.UserID;
                    HttpContext.Current.Session["LoginID"] = ed.Encrypt(id.ToString());  
                    return "{\"msg\":\"success\"}";
                }
                else
                {
                    return "{\"msg\":\"Please make sure you complete varification process if yes then contact admin.!\"}";
                }
            }
            return "{\"msg\":\"Wrong username or password.!\"}";
        }
        catch (Exception e)
        {
            return "{\"msg\":'" + e.Message + "'}";
        }
    }

I am Getting Error Undefined.. One More thing..My webservice.asmx file is in root folder and webservice.cs file in APP_CODE..Is it because of that?

Upvotes: 0

Views: 804

Answers (1)

CompanyDroneFromSector7G
CompanyDroneFromSector7G

Reputation: 4517

Change your script as follows:

function Login() {
    var UserName = $("[id*=tbUserName]").val();
    var PassWord = $("[id*=tbPassword]").val();
    var wsUrl = '<%= ResolveUrl("~/WebServiceLogin.asmx/LoginUser") %>';
    alert(wsUrl);
    $.ajax({
        type: "POST",
        url: wsUrl,
        data: "{'username':'" + UserName + "','password':'" + PassWord + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var j = jQuery.parseJSON(response.d);
            if (j["msg"] == "success")
                window.location = 'UserDashHome.aspx';
            else {
               window.location = 'Login.aspx';
            }
        },
        error: function (response) {
            alert("error:" + response.data + response.d);
        }
    });
}

This will show you if your Url is correct.

As I said in the comment, it will be blank if your script is not embedded directly in the page.

Upvotes: 1

Related Questions