我已經和阿賈克斯和C#方法阿賈克斯重定向到另一個頁面......C#,通過靜態方法
我需要重定向到靜態Web方法的另一頁..
但我不能這樣做。
方法重定向到另一頁是follwoing
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;
}
}
}
錯誤k是
[System.Threading.ThreadAbortException] = {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}
是的,你是ccorrect ..我解決了我的問題,通過更改靜態字符串並返回值爲ajax函數 – 2015-04-06 20:08:34