jquery ajax请求的响应时间不同

时间:2011-07-27 15:11:06

标签: jquery asp.net asp.net-mvc-3

我正在对发布数据并返回json的asp.net mvc控制器进行ajax调用。

这可以正常工作,但我收到以下响应时间。它们每次都不一样,但这给了一个球场指示最新情况。

  • 请求第一次控制器= 2秒
  • 请求第二次控制器= 4秒
  • 请求第3次控制器= 10秒
  • 请求第4次控制器= 30秒
  • 第五次控制器是 要求= 60秒

  • 我目前没有使用mvc 3异步功能或线程。

  • 我正在使用nhibernate

控制器

    [Authorize]
    [HttpPost]
    public JsonResult Index([DefaultValue(0)]int id, UserSession userSession)
    {
        try
        {
            var productId = int.Parse(Request.QueryString["productId"]);
            var company = _companyRepository.Get(userSession.CompanyId);
            var previousProductTotal = decimal.Parse(Request.Form[string.Format("collectivetotalprice-{0}", productId)]);
            var auditOutput = new StringBuilder();
            var campaign = _campaignRepository.Get(id);
            campaign.SubTotal = (campaign.SubTotal - previousProductTotal);
            var product = _productRepository.Get(productId);
            var productCampaigns =
                _productCampaignRepository.FindByCompanyIdAndCampaignIdAndProductId(company.Id,
                                                                                    campaign.Id, productId);



            var totalQuantity = 0;
            foreach (var campaignRetailer in campaign.CampaignRetailers)
            {
                var retailerName = campaignRetailer.Retailer.Name.Replace(" ", "").ToLower();
                var inputRetailerOrder = string.Format("input-retailerorder-{0}", campaignRetailer.Retailer.Id);
                var orderId = int.Parse(Request.Form[inputRetailerOrder]);
                var order = _orderRepository.Get(orderId);

                foreach (var productCampaign in productCampaigns)
                {
                    var inputName = string.Format("input-{0}-{1}", retailerName, productCampaign.Product.Id);
                    var input = Request.Form[inputName];
                    var quantity = StringHelper.IntParse(input);
                    if (quantity > 0) totalQuantity += quantity;
                    int previousQuantity = 0;
                    var orderItem = _orderItemRepository.GetByCompanyIdAndCampaignIdAndRetailerId(company.Id, campaign.Id, campaignRetailer.Retailer.Id, productCampaign.Product.Id);
                    if (orderItem == null)
                    {
                        orderItem = OrderHelper.CreateCampaignOrderItem(order, productCampaign.Product, company, 
                            campaign, campaignRetailer.Retailer, quantity);

                        order.AddOrderItem(orderItem);
                        _orderRepository.Update(order);
                    }
                    else
                    {
                        previousQuantity = orderItem.Quantity;
                        orderItem.Quantity = quantity;
                        _orderItemRepository.Update(orderItem);
                    }

                    AuditHelper.UpdatedOrderItems(orderItem.Quantity, previousQuantity, orderItem.Product.Name,
                                                  retailerName, auditOutput);

                }
            }

            var priceBreak = PriceBreakHelper.Choose(totalQuantity, product);
            var response = new CampaignOrderResponse();
            var productTotal = priceBreak.Price*totalQuantity;
            campaign.SubTotal += productTotal;
            _campaignRepository.Update(campaign);

            var auditMessage = (!string.IsNullOrEmpty(auditOutput.ToString()))
                                   ? string.Format("<table class='pc-100'>{0}</table>", auditOutput.ToString())
                                   : auditOutput.ToString();
            var userProfile = _userProfileRepository.Get(userSession.UserName);
            UpdateAudit(company, campaign, userProfile, auditMessage);

            response.CollectiveQuantity = totalQuantity.ToString();
            response.CollectivePrice = StringHelper.Currency(priceBreak.Price);
            response.CollectivePriceFormatted = priceBreak.Price.ToString();
            response.CollectiveTotalPrice = productTotal.ToString();
            response.CollectiveTotalPriceFormatted = StringHelper.Currency(productTotal);
            response.Production = StringHelper.Currency(campaign.SubTotal);
            response.TotalProduction = StringHelper.Currency(campaign.TotalProduction);
            response.Delivery = StringHelper.Currency(campaign.Delivery);
            response.Variant = StringHelper.Currency(campaign.Variant);
            response.Status = "Updated";
            return this.Json(response, JsonRequestBehavior.AllowGet);

        }
        catch (Exception ex)
        {
            var response = new CampaignOrderResponse();
            response.Status = "UpdateProduct: " + ex.Message;
            return this.Json(response, JsonRequestBehavior.AllowGet); ;
        }
    }

$.ajax({ type: "POST", url: quickResponseUrl, data: $("form").serialize(), success: function (data) { if (data.Status == "Updated") { $(currentButton).html(complete); var collectquantity = priceElement + " #collectivequantity-" + productId; var collectiveprice = priceElement + " #collectiveprice-" + productId; var collectivepriceformatted = priceElement + " #collectivepriceformatted-" + productId; var collectivetotalpriceformatted = priceElement + " #collectivetotalpriceformatted-" + productId; var collectivetotalprice = priceElement + " #collectivetotalprice-" + productId; //alert(collectivetotalprice); $(collectquantity).html(data.CollectiveQuantity); $(collectiveprice).html(data.CollectivePrice); $(collectivepriceformatted).html(data.CollectivePriceFormatted); $(collectivetotalpriceformatted).html(data.CollectiveTotalPriceFormatted); $(collectivetotalprice).val(data.CollectiveTotalPrice);

$("#campaign-totalproduction").html(data.TotalProduction); $("#campaign-delivery").html(data.Delivery); $("#campaign-variant").html(data.Variant); $("#campaign-production").html(data.Production); UpdateRetailers(); console.log(new Date() + ": First Request :" + data.Status); } else { $(".wrapper-matrixStatus").html("<span class='error campaign-status'>Error with first submission " + data.Status + "</span>").show(); $(currentButton).html(complete); } }, error: function (data) { $(".wrapper-matrixStatus").html("<span class='error campaign-status'>Error with first submission " + data.Status + "</span>").show(); $(currentButton).html(complete); } });

0 个答案:

没有答案
相关问题