title: Ajax POST call to ASP.NET MVC controller giving net::ERR_CONNECTION_RESET
body: <p>I am at my wits end about this problem. I've created an ASP.NET MVC 5 website that I am developing and running locally. I've enabled SSL on the site. I've created a self-signed certificate for the site. When I make an ajax POST call to a MVC controller: </p>
  
  <pre><code>$.ajax({
      url: "/Shop/AddToCart/" + id,
      contentType: "application/json; charset=utf-8",
      type: "POST",
      accepts: {
          json: "application/json, text/javascript"
      },
      statusCode: {
          200: function(data) {
              $("#successAlert").show();
              $(function () {
                  var returnObject = data;
                  layoutVM.addProduct(returnObject);
              });
              },
          400: function() {
              $("#errorAlert").show();
              }
      }
  });
  </code></pre>
  
  <p>I get the following error in the JavaScript console in Chrome: "net::ERR_CONNECTION_RESET". It doesn't work in any other browser either. </p>
  
  <p>I know this error has something to do with SSL. As I said, I've created a valid certificate for this site. Unless I'm missing something, my tools (Chrome dev tools, Glimpse, Fiddler) are not telling me anything useful. </p>
  
  <p>Any ideas?</p>
  
  <p><strong>Update (13 March 2015):</strong></p>
  
  <p>So upon further investigation, I found that the MVC controller action is indeed being called. In that method, I am returning an instance of HttpStatusCodeResult:</p>
  
  <pre><code>[HttpPost]
  public ActionResult AddToCart(int id)
  {
      int numChanges = 0;
      var cart = ShoppingCart.GetCart(httpContextBase);
      Data.Product product = null;
      _productRepository = new ProductRepository();
  
      product = _productRepository.GetProducts()
            .Where(x =&gt; x.ProductID == Convert.ToInt32(id)).FirstOrDefault();
  
      if (product != null)
      {
          numChanges = cart.AddToCart(product);
      }
  
      if (numChanges &gt; 0)
      {
          JToken json = JObject.Parse("{ 'id' : " + id + " , 'name' : '" +  
                        product.Name + "', 'price' : '" + product.Price + "', 
                        'count' : '" + numChanges + "' }");
          return new HttpStatusCodeResult(200, json.ToString());
      }
      else
      {
          return new HttpStatusCodeResult(400, "Product couldn't be added to the cart");
      }
  </code></pre>
  
  <p>}</p>
  
  <p>After the method returns with a HTTP 200 code, then I get the "net:: ERR_CONNECTION_RESET" in Chrome (and error in other browsers). It's important to note that the 200 code handler in the jQuery .ajax call is never called. the connection is reset immediately upon returning. </p>
  
  <p>According to some blogs, I should increase the maxRequestLength, which I have:</p>
  
  <pre><code>&lt;system.web&gt;
      &lt;httpRuntime targetFramework="4.5" 
                   maxRequestLength="10485760" executionTimeout="36000" /&gt;
  &lt;/system.web&gt;
  </code></pre>
  
  <p>But this hasn't worked.</p>
  
  <p><strong>Update (13 March 2015):</strong></p>
  
  <p>So I changed the $.ajax call to respond to success and error as opposed to specific status codes like so:</p>
  
  <pre><code>$.ajax({
      url: "/Shop/AddToCart/" + id,
      contentType: "application/json; charset=utf-8",
      type: "POST",
      accepts: {
          json: "application/json, text/javascript"
      },
      success: function (data, textStatus, jqXHR) {
          // jqXHR.status contains the Response.Status set on the server
          alert(data);
      },
      error: function (jqXHR, textStatus, errorThrown) {
          // jqXHR.status contains the Response.Status set on the server
          alert(jqXHR.statusCode + ": " + jqXHR.status);
      }
  });
  </code></pre>
  
  <p>Now, even though I am returning back a 200 from my controller code, the error block is being hit. So, that's progress. BUT, the textStatus is simply "error" and jqXHR.status is simply 0. </p>
  
  <p>Any ideas?</p>