zoukankan      html  css  js  c++  java
  • aspx页面使用ajax遇到try catch中使用Response.End()报错

    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);
    }
    }
  • 相关阅读:
    XML操作类
    输入框样式总结
    根据计算机MAC地址限定每台机子只能领取一次账号
    ico图标的应用
    C#实现关机功能
    在sql中实现数组
    JSON
    MvcHtml.ActionLink()用法
    Brettle.Web.NeatUpload.dll 大文件上传
    asp.net 创建Access数据库
  • 原文地址:https://www.cnblogs.com/wifi/p/6697939.html
Copyright © 2011-2022 走看看