【问题标题】:jQuery-Ajax doesn't send datajQuery-Ajax 不发送数据
【发布时间】:2020-09-19 10:45:02
【问题描述】:

我将 asp.net 核心用于我的项目后端,但我的方法没有获取我通过 jQuery ajax 发送给他们的数据

这些是我发送数据的脚本:

function ChangeCount(productId, count) {
    $.ajax({
        type: "POST",
        data: JSON.stringify({ ProductId: productId, Count: count }),
        url:'@(Url.Action("ChangeOrderCount", "Payment"))',
        contentType: "application/json;",
        dataType:"json"
    }).done(function (result) {
        if(!isNaN(result)) {
            var NewProductCount = parseInt(result)
            if (NewProductCount == 0) {
                $("#Order-" + productId).fadeOut();
            }
            else {
                $("#OrderCount-" + productId).html(NewProductCount);
            }
        } else {
            alert(result);
        }
    });
}

这是我的后端代码:

public JsonResult ChangeOrderCount(int ProductId, int Count)
{
    string userId = userRepository.GetUserByEmail(User.Identity.Name).Id;
    bool result = bascketrepository.AddToCart(userId, ProductId, Count);
    if (result)
        return Json(bascketrepository.GetProductOrderCountInCurrentBascket(userId,ProductId));
    else
        return Json("خطایی رخ داده است");
}

【问题讨论】:

  • 你能做两件事吗?首先向我们展示后端方法代码。第二个在你的ChangeCount函数开始时,做alert(productID)alert(count)
  • 我将它们登录到控制台然后我确定我的 js 函数获取了值并发送了 ajax 请求但它不发送数据!
  • 您可以将代码添加为文本而不是图像吗?

标签: javascript jquery asp.net ajax asp.net-core


【解决方案1】:

在你的ajax方法中,contentType是“application/json”,所以控制器不能分析数据。如果你想让ajax传递数据给控制器,你需要改变ajax中的contenttype。 这是我的控制器和视图,它有效。

控制器:

[HttpPost]
        public JsonResult ChangeOrderCount(int ProductId, int Count)
        {
            Console.WriteLine(ProductId + "," + Count);
            return Json("success");
        }
        [HttpGet]
        public IActionResult ChangeOrderCount()
        {
            return View();
        }

查看:

@section scripts{ 
    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            var productId = 1;
            var count = 2;
            ChangeCount(productId, count);
        })
        function ChangeCount(productId, count) {
            $.ajax({
                type: "POST",
                data: { ProductId: productId, Count: count },
                url: '@(Url.Action("ChangeOrderCount", "Payment"))',
                contentType: "application/x-www-form-urlencoded",
            }).done(function (result) {
                if (!isNaN(result)) {
                    var NewProductCount = parseInt(result)
                    if (NewProductCount == 0) {
                        $("#Order-" + productId).fadeOut();
                    }
                    else {
                        $("#OrderCount-" + productId).html(NewProductCount);
                    }
                } else {
                    alert(result);
                }
            });
        }


    </script>
}

结果如下: 或者您可以更改控制器的代码:

型号:

public class ProductCount
    {
        public int ProductId { get; set; }
        public int Count { get; set; }
    }

控制器:

[HttpPost]
        public JsonResult ChangeOrderCount([FromBody]ProductCount productCount)
        {
            Console.WriteLine(productCount.ProductId + "," + productCount.Count);
            return Json("success");
        }
        [HttpGet]
        public IActionResult ChangeOrderCount()
        {
            return View();
        }

查看:

$(function () {
        var productId = 1;
        var count = 2;
        ChangeCount(productId, count);
    })
    function ChangeCount(productId, count) {
        $.ajax({
            type: "POST",
            data: JSON.stringify({ ProductId: productId, Count: count }),
            url: '@(Url.Action("ChangeOrderCount", "Payment"))',
            contentType: "application/json",
        }).done(function (result) {
            if (!isNaN(result)) {
                var NewProductCount = parseInt(result)
                if (NewProductCount == 0) {
                    $("#Order-" + productId).fadeOut();
                }
                else {
                    $("#OrderCount-" + productId).html(NewProductCount);
                }
            } else {
                alert(result);
            }
        });
    }

结果如下:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-01
    • 2014-10-10
    • 2014-04-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-20
    • 1970-01-01
    相关资源
    最近更新 更多