我有和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] 删除。
我来说两句