js->
var favoriteIcons = document.querySelectorAll(".favoriteIcon");
favoriteIcons.forEach((item) => {
item.addEventListener("click", function() {
var itemPPElement = item.parentElement.parentElement;
var id = itemPPElement.id;
var customer_id = itemPPElement.querySelector(".customer_id").value;
var product_id = itemPPElement.querySelector(".product_id").value;
var jsonObject = {
favorite_id: id,
customer_id: customer_id,
product_id: product_id
};
console.log(jsonObject);
fetch('/Favorite/SwitchFavorite', {
method: "POST",
body: JSON.stringify(jsonObject),
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json' }
})
.then(response => response.json())
.then(json => {
item.classList.toggle("la-heart");
item.classList.toggle("la-heart-o");
console.log(json);
})
.catch(err => console.log(err));
});
});
控制器->
[HttpPost]
public JsonResult SwitchFavorite(Favorite favorite)
{
return Json(favorite);
}
我会在控制器中捕获我的json数据并处理它,但是我直接返回传入的数据来控制我的数据,但它不捕获数据。我该怎么办?
尝试改变
[HttpPost]
public JsonResult SwitchFavorite(Favorite favorite)
{
return Json(favorite);
}
到
[HttpPost]
public JsonResult SwitchFavorite([FromBody]Favorite favorite)
{
return Json(favorite);
}