1、使用Ajax接收數據,在返回Response.Write()后應該調用Response.End()才能將數據寫入到調用的頁面,才能被jQuery的回調函數獲取到返回的JSON數據
2、在try--catch里面不能用Response.End(),否則會報錯:由於代碼已經過優化或者本機框架位於調用堆棧之上,無法計算表達式的值。
在調用Response.End()時,會執行Thread.CurrentThread.Abort()操作。
如果將Response.End()放在try...catch中,catch會捕捉Thread.CurrentThread.Abort()產生的異常System.Threading.ThreadAbortException。
解決方法(任選一個):
1. 在catch中排除ThreadAbortException異常,示例代碼如下:
try
{
Response.End();
}
catch (System.Threading.ThreadAbortException)
{
}
catch (Exception ex)
{
Response.Write(ex);
}
2. 用Context.ApplicationInstance.CompleteRequest()結束當前請求,代碼如下:
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);
}
}