Javascript jqueryajax没有';不要发送数据
我使用asp.NETCore作为项目的后端,但我的方法没有获得我通过jQueryAjax发送给他们的数据 以下是我发送数据的脚本:Javascript jqueryajax没有';不要发送数据,javascript,jquery,asp.net,ajax,asp.net-core,Javascript,Jquery,Asp.net,Ajax,Asp.net Core,我使用asp.NETCore作为项目的后端,但我的方法没有获得我通过jQueryAjax发送给他们的数据 以下是我发送数据的脚本: function ChangeCount(productId, count) { $.ajax({ type: "POST", data: JSON.stringify({ ProductId: productId, Count: count }), url:'@(Url.Action("ChangeOrde
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("خطایی رخ داده است");
}
在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();
}
[HttpPost]
public JsonResult ChangeOrderCount([FromBody]ProductCount productCount)
{
Console.WriteLine(productCount.ProductId + "," + productCount.Count);
return Json("success");
}
[HttpGet]
public IActionResult ChangeOrderCount()
{
return View();
}
视图:
控制器:
[HttpPost]
public JsonResult ChangeOrderCount(int ProductId, int Count)
{
Console.WriteLine(ProductId + "," + Count);
return Json("success");
}
[HttpGet]
public IActionResult ChangeOrderCount()
{
return View();
}
[HttpPost]
public JsonResult ChangeOrderCount([FromBody]ProductCount productCount)
{
Console.WriteLine(productCount.ProductId + "," + productCount.Count);
return Json("success");
}
[HttpGet]
public IActionResult ChangeOrderCount()
{
return View();
}
视图:
这就是结果:
你能做两件事吗。首先向我们展示后端方法代码。第二,在您的
changecont
函数开始时,执行alert(productID)
和alert(count)
我将它们记录在控制台中,然后我确定我的js函数获取值并发送ajax请求,但它不发送数据!能否将代码添加为文本而不是图像?
$(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);
}
});
}