簡體   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