繁体   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

我正在开发一个ASP.NET Web应用程序。 在登录屏幕上,我使用jQuery Ajax调用连接服务器端代码。 在ajax调用的响应中,我获取整个HTML页面,而不是true或false(我希望从响应中获得实际结果)。

下面是我的服务器代码。

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

以下是我的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
    }
});

我在我的Ajax调用中包含了contentType。 它解决了我的问题。 现在,我得到了预期的JSON响应。 希望这会有所帮助。感谢那些帮助我的人。

暂无
暂无

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

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