【问题标题】:I'm using ajax call in ASP.NET application and I'm getting entire HTML page as response from jquery Ajax call我在 ASP.NET 应用程序中使用 ajax 调用,并且我正在获取整个 HTML 页面作为 jquery Ajax 调用的响应
【发布时间】:2017-09-18 20:30:46
【问题描述】:

我正在开发一个 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
    }
});

【问题讨论】:

  • 向我们展示您的代码
  • 更新请帮帮我。提前致谢
  • 和你的回复文字?
  • 你确定服务器端代码被执行了吗?你调试了吗? Web 方法未在您的代码中配置为 post call。您在 Ajax 响应中收到的 html 是什么?
  • AJAX调用成功时data变量持有什么样的字符串?使用 success: function(data) { console.log(data); ... } 显示从浏览器 JS 控制台收集的 HTML 页面标记。

标签: javascript jquery asp.net ajax


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    • 1970-01-01
    • 2020-11-27
    • 2014-02-21
    • 1970-01-01
    • 2013-07-09
    • 2013-12-06
    相关资源
    最近更新 更多