aspx页面使用ajax遇到try catch中使用Response.End()报错

一、使用Ajax接收数据,在返回Response.Write()后应该调用Response.End()才能将数据写入到调用的页面,才能被jQuery的回调函数获取到返回的JSON数据框架

二、在try--catch里面不能用Response.End(),不然会报错:因为代码已通过优化或者本机框架位于调用堆栈之上,没法计算表达式的值。函数

 

在调用Response.End()时,会执行Thread.CurrentThread.Abort()操做。优化

若是将Response.End()放在try...catch中,catch会捕捉Thread.CurrentThread.Abort()产生的异常System.Threading.ThreadAbortException。this

解决方法(任选一个):spa

1. 在catch中排除ThreadAbortException异常,示例代码以下:code

try
{
Response.End();
}
catch (System.Threading.ThreadAbortException)
{
}
catch (Exception ex)
{
Response.Write(ex);
}

2. 用Context.ApplicationInstance.CompleteRequest()结束当前请求,代码以下:blog

protected void Page_Load(object sender, EventArgs e){    try    {        Response.Write("Hello world!");        this.Page.Visible = false;        Context.ApplicationInstance.CompleteRequest();    }    catch (Exception ex)    {        Response.Write(ex);    }}
相关文章
相关标签/搜索