【问题标题】:how to get json data from asp.net controller using ajax in jquery如何在 jquery 中使用 ajax 从 asp.net 控制器获取 json 数据
【发布时间】:2018-07-13 16:03:21
【问题描述】:

我开始参与,我有一个控制器,并且我尝试通过 jquery ajax 以 json 格式获取数据。 我将数据作为 List , string ,...但作为模型?没有永不。 我多次使用这种类型的代码,但今天它不起作用。天哪。 控制器

[HttpPost]
    public JsonResult UpdatedShoppingCartRentalItems()
    {
        var subTotalIncludingTax = _workContext.TaxDisplayType == TaxDisplayType.IncludingTax && !_taxSettings.ForceTaxExclusionFromOrderSubtotal;
        var shoppingCartItems = _workContext.CurrentCustomer.ShoppingCartItems;
        var model = new List<RentalMiniCart>();
        if (shoppingCartItems.Any())
        {
            foreach (var item in shoppingCartItems)
            {
                var product = _productService.GetProductById(item.ProductId);
                var row = new RentalMiniCart()
                {
                    ShoppingCartItem = item,
                    ProductSeName = product.GetSeName()
                };
                if (item.RentalStartDateUtc != null && item.RentalEndDateUtc != null)
                {
                    // rental product
                    // number of days
                    var numberofDays = 1;
                    if (item.RentalStartDateUtc != item.RentalEndDateUtc)
                    {
                        //endDate = endDate.AddDays(-1);
                        var numberOfDaysTimeSpan = item.RentalEndDateUtc - item.RentalStartDateUtc;
                        numberofDays = numberOfDaysTimeSpan.Value.Days;
                    }
                    var previousDecimalPrice = numberofDays * product.Price;
                    row.PreviousPrice = _priceFormatter.FormatPrice(previousDecimalPrice, false, _workContext.WorkingCurrency, _workContext.WorkingLanguage, subTotalIncludingTax);
                    var currentDecimalPrice = RentalSystemHelper.CalculateRentalPrice(product.Id, item.RentalStartDateUtc, item.RentalEndDateUtc);
                    row.CurrentPrice = _priceFormatter.FormatPrice(currentDecimalPrice, false, _workContext.WorkingCurrency, _workContext.WorkingLanguage, subTotalIncludingTax);
                }
                else
                {
                    row.PreviousPrice = _priceFormatter.FormatPrice(product.Price, false, _workContext.WorkingCurrency, _workContext.WorkingLanguage, subTotalIncludingTax);
                    row.CurrentPrice = _priceFormatter.FormatPrice(product.Price, false, _workContext.WorkingCurrency, _workContext.WorkingLanguage, subTotalIncludingTax);
                }
                model.Add(row);
            }
        }
        return Json(model);
    }

我使用断点并检测模型有值,但我得到错误 json。

我的观点

function CorrectMiniCartItems() {
    $.ajax({
        type: 'POST',
        url: "@Html.Raw(Url.Action("UpdatedShoppingCartRentalItems", "MiscNopshopRentalSystem"))",
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        /*data: data,*/
        success: function (result) {
            console.log("success result: " + result);
            // Code goes here
        },
        error: function(result) {
            console.log("error result: "+result);

        }
        ,
        complete : function (result) {
            //console.log("complete result: " + result);
        }
    });
}

【问题讨论】:

标签: jquery asp.net ajax model-view-controller nopcommerce-4.0


【解决方案1】:

尝试从

更改您的退货
return Json(model)

return Json(model, JsonRequestBehavior.AllowGet)

【讨论】:

  • 我之前已经测试过了。结果是一样的,错误
【解决方案2】:

正如@JamesS https://github.com/Tratcher/EDES/blob/a5e783cf4d1689590a07f10c1a48ffc7f0981352/EDES/Controllers/ErrorController.cs#L43评论中提到的那样

这行得通,

[HttpPost]
        [Authorize(AuthenticationSchemes = ApiKeyAuthDefaults.AuthenticationScheme)]
        public IActionResult Post([FromBody]JObject body)
        {
            if (body == null)
            {
                return BadRequest();
            }
            
            var report = new ErrorReport()
            {
                Created = DateTimeOffset.UtcNow,
                Message = body.GetValue("message").Value<string>(),
                Version = body.GetValue("version").Value<string>(),
                Json = body.GetValue("json").ToString(Formatting.None),
            };

            _context.ErrorReports.Add(report);
            _context.SaveChanges();

            return Ok();
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-30
    • 1970-01-01
    • 1970-01-01
    • 2020-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    相关资源
    最近更新 更多