I have a simple code as below, but the productArray in axios always returns null.
It works when I do it with jquery. what am i missing?
$.post(`/api/${productId}/getProducts`, { products: productArray })
[HttpPost]
public async Task<IActionResult> GetProducts(int productId, Request request)
public class Request
{
public List<ProductDetails> Products { get; set; }
}
public class ProductDetails
{
public int ProductId { get; set; }
public int Price { get; set; }
}
async function GetAllProducts() {
let productArray = [];
productArray.push({ productId: "1", price: "5" });
const response = await axios.post(`/api/${productId}/getProducts`, productArray)
}
The "thing" you are sending with jQuery is an object that contains a single key/value pair of products: productArray but what you are sending in axios is only the productArray, not the object containing the array. if you create the object and send that, your axios call would match your jQuery call
async function GetAllProducts() {
let productArray = [];
productArray.push({ productId: "1", price: "5" });
let o = { products: productArray };
const response = await axios.post(`/api/${productId}/getProducts`, o)
}