简体   繁体   中英

I'm using ajax call in ASP.NET application and I'm getting entire HTML page as response from jquery Ajax call

I'm developing an ASP.NET web application. Where in login screen I used jQuery Ajax call to connect server side code. In the response from the ajax call Im getting entire HTML page instead of true or false ( the actual result I want from the response).

Below is my server code.

[WebMethod]
    public static string LoginValidation(string userName, string passWord)
    {
        string connectionString = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand("select * from LoginUser where username = '"
            + userName + "' and password = '" + passWord + "'");
        SqlDataReader rdr = cmd.ExecuteReader();
        if (rdr.HasRows)
        {
            return "succes";
        }
        return "failure";
    }        

Below is my Jquery Ajax call.

 $(document).ready(function () {
    $('#txtLogin').focus();
});

$('#btnLogin').click(function () {
    var username = $('#txtLogin').val();
    var password = $('#txtPassword').val();

    if (username === "" || password === "") {
        alert('Both the fields are mandatory. Kindly try again');
        $('#txtLogin').focus();
        return;
    }
    else {
        //ajax call to validate the login
        $.ajax({
            type: 'POST',
            data: { 'userName': username, 'passWord': password },
            url: 'Default.aspx/LoginValidation',
            async: true,
            datatype: 'text',
            success: function (data) {
            alert('test sadfadfa '+data+' '+data.responseText);
                if (data == "failure") {
                    //redirecting to master page once after the successfull login
                    window.location.href = "/masterscreen.aspx";
                }
                else {
                    alert('false');
                }
                return;
            },
            error: function (data) {

            }
        });
        //ajax call end
    }
});

i've included the contentType in my Ajax call. It resolved my problem. Now, I'm getting JSON response as expected. Hope this helps some one.Thanks for those who helped me out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM