简体   繁体   English

如何从 asp.net 中的 jquery ajax 重定向到新页面

[英]how to redirect to a new page from jquery ajax in asp.net

my web services works fine.我的 web 服务工作正常。 it gives me the "success" msg.它给了我“成功”的信息。 but when i execute it through jquery ajax, i do not get the appropriate result.但是当我通过 jquery ajax 执行它时,我没有得到适当的结果。 kindly correct me... I am not getting redirected, where else i get "invalid user"...请纠正我......我没有被重定向,我在哪里得到“无效用户”......

web service: web 服务:

[WebMethod]
public string Retrieve1(string username)
    {
        SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-3GFU533;Initial Catalog=employee;Integrated Security=True");
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from employee1 where Name =@username", con);
        cmd.Parameters.AddWithValue("@username", username);
        //cmd.Parameters.AddWithValue("@password", txtPWD.Text);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            return "success";
        }
        else
        {
            return "error";
        }
    }

jquery ajax function: jquery ajax function:

<script type="text/javascript">
    $(document).ready(function () {
        $('#btnsubmit').click(function () {
            var username = $('#username').val();
            $.ajax({
                type: 'GET',
                contentType: "application/json; charset=utf-8",
                url: "Retrieve.asmx/Retrieve1",
                data: "{'username':'" + username + "'}",
                dataType: "json",
                async:true,
                success: function (data) {
                    var result = data.d;
                    if (result == "success")
                    { window.location.href = "Details.aspx"; }   
                },
                error: function () {
                    alert("Invalid Username");
                }
            });
        });
    });
</script>

Just you must change your js code to this:只是您必须将您的js代码更改为:

If you are using json data type.如果您使用的是json数据类型。 your response must be as json.您的回复必须为 json。

ex: {"status":"success"}例如: {"status":"success"}

$(document).ready(function () {
    $('#btnsubmit').click(function () {
        var username = $('#username').val();
        $.ajax({
            type: 'GET',
            contentType: "application/json; charset=utf-8",
            url: "Retrieve.asmx/Retrieve1",
            data: "{'username':'" + username + "'}",
            dataType: "json",
            async:true,
            success: function (data) {
                if (result.status == "success")
                { window.location.replace("Details.aspx"); }   
            },
            error: function () {
                alert("Invalid Username");
            }
        });
    });
});

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

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