AJAX POST call for ASP.NET MVC controller providing net :: ERR_CONNECTION_RESET

I am aloof from this problem. I created an ASP.NET MVC 5 website that I develop and work locally. I have enabled SSL on the site. I created a self-signed certificate for the site. When I make an ajax POST call to the MVC controller:

$.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();
            }
    }
});

I get the following error in the JavaScript console in Chrome: "net :: ERR_CONNECTION_RESET". It does not work in any other browser.

I know this error has something to do with SSL. As I said, I created a valid certificate for this site. If I am missing something, my tools (Chrome dev, Glimpse, Fiddler tools) do not tell me anything useful.

Any ideas?

Update (March 13, 2015):

, , MVC . HttpStatusCodeResult:

[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 => 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 + "' }");
        return new HttpStatusCodeResult(200, json.ToString());
    }
    else
    {
        return new HttpStatusCodeResult(400, "Product couldn't be added to the cart");
    }

}

, HTTP 200, "net:: ERR_CONNECTION_RESET" Chrome ( ). , 200 jQuery.ajax . reset .

, maxRequestLength, :

<system.web>
    <httpRuntime targetFramework="4.5" 
                 maxRequestLength="10485760" executionTimeout="36000" />
</system.web>

.

(13 2015 .):

, $.ajax, , , :

$.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);
    }
});

, 200 , . , . , textStatus - "", jqXHR.status - 0.

?

+4
3

. , , , HttpStatusCodeResult reset. JToken :

[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("{}");
    }
}

.

, , . .

+2

. \r\n. , statusDescription HttpStatusCodeResult . ( , ) , , , .

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

, - !:)

+1

, , SSL, , , , :

[HttpGet]
public IActionResult GetContact(int contactId)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    Contact contact = _contactRepository.Get(contactId);

    if (contact == null)
    {
        return NotFound();
    }

    return Ok(new {contact.FirstName, contact.LastName);
}

, , , , , . .

0
source

All Articles