Ajax POST call to ASP.NET MVC controller giving net::ERR_CONNECTION_RESET

I have had this same problem. In my situation, the inner exception message contained a \r\n character. After testing, I realized that the statusDescription parameter in HttpStatusCodeResult did not like this. (I'm not sure why) I simply used the code below to remove the characters and everything then worked as expected.

exception.Message.Replace("\r\n", string.Empty);

Hopefully this will help someone else! :)


I have solved the issue. I don't understand why this is, but it seems as though the more robust solution of returning an instance of HttpStatusCodeResult is what was causing the connection reset. When I set the Response status code and return a JToken object like so:

[HttpPost]
public JToken AddToCart(int id)
{
    int numChanges = 0;
    var cart = ShoppingCart.GetCart(httpContextBase);
    Data.Product product = null;
    _productRepository = new ProductRepository();

    product = _productRepository.GetProducts()
       .Where(x => x.ProductID == Convert.ToInt32(id)).FirstOrDefault();

    if (product != null)
    {
        numChanges = cart.AddToCart(product);
    }

    if (numChanges > 0)
    {
        JToken json = JObject.Parse("{ 'id' : " + id + " , 'name' : '" + 
                    product.Name + "', 'price' : '" + product.Price + "', 
                    'count' : '" + numChanges + "' }");

        Response.StatusCode = 200;
        return json;
    }
    else
    {
        Response.StatusCode = 400;
        Response.StatusDescription = "Product couldn't be added to the cart";
        return JObject.Parse("{}");
    }
}

Everything works just fine.

I would LOVE to understand why. But, for now, that's my solution.