Ajax POST调用ASP.NET MVC控制器给出net :: ERR_CONNECTION_RESET

ssh*_*ley 6 ajax asp.net-mvc ssl-certificate

关于这个问题,我的斗智斗勇.我创建了一个ASP.NET MVC 5网站,我正在本地开发和运行.我在网站上启用了SSL.我为该网站创建了一个自签名证书.当我对MVC控制器进行ajax POST调用时:

$.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();
            }
    }
});
Run Code Online (Sandbox Code Playgroud)

我在Chrome的JavaScript控制台中收到以下错误:"net :: ERR_CONNECTION_RESET".它也不适用于任何其他浏览器.

我知道这个错误与SSL有关.正如我所说,我为这个网站创建了一个有效的证书.除非我遗漏了什么,否则我的工具(Chrome开发工具,Glimpse,Fiddler)并没有告诉我任何有用的东西.

有任何想法吗?

更新(2015年3月13日):

因此,经过进一步调查,我发现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");
    }
Run Code Online (Sandbox Code Playgroud)

}

在方法返回HTTP 200代码后,我在Chrome中获得"net :: ERR_CONNECTION_RESET"(在其他浏览器中出错).重要的是要注意,jQuery .ajax调用中的200代码处理程序永远不会被调用.返回后立即重置连接.

根据一些博客,我应该增加maxRequestLength,我有:

<system.web>
    <httpRuntime targetFramework="4.5" 
                 maxRequestLength="10485760" executionTimeout="36000" />
</system.web>
Run Code Online (Sandbox Code Playgroud)

但这没效果.

更新(2015年3月13日):

所以我更改了$ .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);
    }
});
Run Code Online (Sandbox Code Playgroud)

现在,即使我从控制器代码返回200,错误块也被击中.那就是进步.但是,textStatus只是"错误"而jqXHR.status只是0.

有任何想法吗?

Cla*_*att 5

我也遇到过同样的问题。在我的情况下,内部异常消息包含\r\n字符。经过测试,我意识到HttpStatusCodeResult中的statusDescription参数不是这样的。(我不知道为什么)我只是使用下面的代码来删除字符,然后一切都按预期工作。

exception.Message.Replace("\r\n", string.Empty);
Run Code Online (Sandbox Code Playgroud)

希望这会帮助别人!:)


ssh*_*ley 2

我已经解决了这个问题。我不明白为什么会这样,但返回 HttpStatusCodeResult 实例的更可靠的解决方案似乎是导致连接重置的原因。当我设置响应状态代码并返回 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("{}");
    }
}
Run Code Online (Sandbox Code Playgroud)

一切都很好。

我很想了解为什么。但是,就目前而言,这就是我的解决方案。