简体   繁体   English

我在ASP.NET应用程序中使用ajax调用,并且从jquery Ajax调用中获取了整个HTML页面作为响应

[英]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. 我正在开发一个ASP.NET Web应用程序。 Where in login screen I used jQuery Ajax call to connect server side code. 在登录屏幕上,我使用jQuery Ajax调用连接服务器端代码。 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). 在ajax调用的响应中,我获取整个HTML页面,而不是true或false(我希望从响应中获得实际结果)。

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. 以下是我的Jquery Ajax调用。

 $(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. 我在我的Ajax调用中包含了contentType。 It resolved my problem. 它解决了我的问题。 Now, I'm getting JSON response as expected. 现在,我得到了预期的JSON响应。 Hope this helps some one.Thanks for those who helped me out. 希望这会有所帮助。感谢那些帮助我的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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