Search code examples
jqueryasp.netajaxt-sqlwebmethod

WebMethod error response as application/json on Development but text/html on Production


On DEV the error(catch) returns application/json, but on LIVE it's 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;
    }

Here's the jquery ajax call:

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

I tried replacing the catch with:

    catch(Excecption ex){ throw ex; }

and It works, but there's an error being ignored.

Any help is greatly appreciated. Thank you in advanced.


Solution

  • It turns out the issue is about "Friendly Error Message" - which is in html format. IIS is using this by default on Windows 2008 R2 machine. As a solution, I found this article: http://helpnotes.vpasp.com/kb/611-General-hosting-questions/1089-How-to-Turn-Off-Friendly-Error-in-IIS-7xx/ and performed #4 to #6.