aspx頁面使用ajax遇到try catch中使用Response.End()報錯

來源:互聯網
上載者:User

標籤:context   無法   obj   解決   try catch   排除   thread   response   hello   

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);
}
}

aspx頁面使用ajax遇到try catch中使用Response.End()報錯

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.