C#,Ajax通过静态方法重定向到另一个页面

乔·达利瓦(Jot Dhaliwal)

我有和ajax和C#的方法...

我需要重定向到静态Web方法中的另一个页面。

但是我做不到。

重定向到另一个页面的方法如下

 if (tempUser == "0")
                {

                    HttpContext.Current.Response.Redirect("login.aspx");
                }
                else
                {
                    HttpContext.Current.Response.Redirect("list.aspx");
                }

AJAX

$('#Button1').click(function () {
                    var HTML = document.getElementById("data").innerHTML;
                //  alert(HTML);
                var Fname = document.getElementById("MyText").value;
                Senddata = { "HTML": HTML, "Fname": Fname };
                $.ajax({
                    type: "Post",
                    url: "/wwwroot/Default.aspx/save",
                    data: JSON.stringify(Senddata),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (result) {


                    }
                });
            }
        });

C#静态Web方法

[WebMethod(EnableSession = true)]
    public static void save(string HTML, string Fname)
    {


        HttpContext.Current.Session["GUID"] = Convert.ToString(Guid.NewGuid());

        try
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["MS"].ConnectionString;
            SqlCommand cmd = new SqlCommand();
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }

            string tempUser = Convert.ToString(HttpContext.Current.Session["UserID"]);

            if (string.IsNullOrEmpty(tempUser))
            {
                tempUser = "0";
            }

            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            cmd.CommandText = "insert into Forms(Name,HTML,UserID,GUID)values(@Name,@HTML,@UserID,@GUID)";
            cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 1024).Value = Fname;
            cmd.Parameters.Add("@HTML", SqlDbType.VarChar, 8000).Value = HTML.Trim();
            cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = tempUser;
            cmd.Parameters.Add("@GUID", SqlDbType.NVarChar, 50).Value = HttpContext.Current.Session["GUID"];

            cmd.ExecuteNonQuery();
            cmd.Dispose();
            con.Close();
            if (tempUser == "0")
            {

                HttpContext.Current.Response.Redirect("login.aspx");
            }
            else
            {
                HttpContext.Current.Response.Redirect("list.aspx");
            }


        }
        catch (Exception ex)
        {

            CommonBLL.WriteExceptionLog(ex, "Form Save Default.aspx");
            throw ex;
        }

    }
}

在catch块中抛出的错误是

[System.Threading.ThreadAbortException] = {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}
前果

我认为您无法在ajax调用期间或之后从服务器端代码重定向。至少您可以返回一个URL(例如Response.write(“ YourURlBuildOnC#”))用于成功函数的重定向

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

自动从一个JSP页面重定向到另一个

React:重定向到另一个页面

如何通过执行JavaScript代码添加动态操作以重定向到另一个页面?

如何将从Ajax请求收到的响应重定向到另一个HTML页面

Django Ajax表单提交错误地重定向到另一个页面

通过AJAX重定向到PHP中的另一个页面

如何通过使用网格视图中的图像按钮重定向到另一个页面?

使用php和ajax重定向到另一个页面

jQuery / Ajax-成功消息后重定向到另一个页面

如何重定向到另一个页面

重定向到另一个HTML页面

如何将一个页面网址重定向到另一个

如何通过使用JavaScript函数重定向到Struts2中的另一个JSP页面

AJAX请求完成后如何重定向到另一个页面

重定向到另一个页面

有条件地重定向到另一个页面,并通过PHP中的POST发送参数

无法从一个页面重定向到另一个页面

C# 函数错误,因为无法重定向到另一个页面

通过ajax获取搜索值后,重定向到另一个View的结果

如何在codeigniter中使用ajax jquery将页面重定向到另一个页面?

如何从angularjs访问节点js post方法以重定向到另一个页面?

在ajax调用中重定向到另一个页面

如何通过单击 iframe 中的按钮将用户重定向到另一个页面

如何通过更改 wp 查询而不重新加载页面从一个页面重定向到另一个页面?

使用 jQuery ajax 将数据从表单发送到电子邮件,并通过传递 POST 变量重定向到另一个页面不起作用

通过服务器重定向到另一个带有 TOKEN 标头的页面

从一个 HTML 页面重定向到另一个页面到一个标题

如何通过按下没有硒的按钮重定向到另一个页面?

如何重定向到另一个页面?