【问题标题】:WebMethod error response as application/json on Development but text/html on ProductionWebMethod 错误响应为开发上的 application/json 但生产上的 text/html
【发布时间】:2013-06-11 19:44:04
【问题描述】:

在 DEV 上,错误(捕获)返回 application/json,但在 LIVE 上它是 text/html:

    [WebMethod]
    public static string UpdateCategoryProductDisplayOrder(int parentCategoryId, int productId, int displayOrder)
    {
        int rowsAffected = 0;
        string message = string.Empty;

        try
        {
            rowsAffected = DataAccess.CategoryProducts.UpdateCategoryProductDisplayOrder(parentCategoryId, productId, displayOrder);
            message = rowsAffected.ToString();
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.StatusCode = 500;
            message = ex.Message;
        }

        return message;
    }

这是 jquery ajax 调用:

    //Call the page method  
return $.ajax({
                    type: "POST",
                    url: "WebMethods.aspx" + "/" + fn,
                    contentType: "application/json; charset=utf-8",
                    data: paramList,
                    dataType: "json",
                    success: successFn,
                    error: errorFn
        });

我尝试将 catch 替换为:

    catch(Excecption ex){ throw ex; }

它可以工作,但有一个错误被忽略。

非常感谢任何帮助。提前谢谢你。

【问题讨论】:

    标签: jquery asp.net ajax tsql webmethod


    【解决方案1】:

    原来问题是关于“友好错误消息”的——它是 html 格式的。 IIS 在 Windows 2008 R2 机器上默认使用这个。作为解决方案,我找到了这篇文章:http://helpnotes.vpasp.com/kb/611-General-hosting-questions/1089-How-to-Turn-Off-Friendly-Error-in-IIS-7xx/ 并执行了 #4 到 #6。

    【讨论】:

      猜你喜欢
      • 2012-02-06
      • 2013-12-02
      • 1970-01-01
      • 2016-08-31
      • 1970-01-01
      • 1970-01-01
      • 2017-08-25
      • 2011-06-17
      • 1970-01-01
      相关资源
      最近更新 更多