如何在mvc和JavaScript中使用异步获取发出post请求?
我正在尝试从我的mvc应用程序中获取子类别,并使用异步获取引用类别id 我已经取得了类别和它的所有工作 但是当我试图用post请求获取子类别时,它不起作用如何在mvc和JavaScript中使用异步获取发出post请求?,javascript,asynchronous,model-view-controller,fetch,Javascript,Asynchronous,Model View Controller,Fetch,我正在尝试从我的mvc应用程序中获取子类别,并使用异步获取引用类别id 我已经取得了类别和它的所有工作 但是当我试图用post请求获取子类别时,它不起作用 //SubCategories const categoriesLiList = document.querySelectorAll('.btn'); const getSubCategories = async () => { const liBt
//SubCategories
const categoriesLiList = document.querySelectorAll('.btn');
const getSubCategories = async () => {
const liBtnClick = list => {
nodeListForEach(list, cur => {
cur.addEventListener('click', () => {
debugger;
let categoryId = cur.value;
console.log(categoryId);
const getSubCategoriesById = async (url = ``, data = {}) => {
const subsResult = await fetch(url, {
method: "POST",
mode: "cors",
cache: "no-cache",
credentials: "same-origin",
headers: {
"Content-Type": "application/json"
},
redirect: "follow",
referrer: "no-referrer",
body: JSON.stringify(data)
});
const subsData = await subsResult.json();
const populateSubCategories = arr => {
arr.forEach(cur => {
const subCategoriesLi = `
<li>${cur.Name}</li>
`;
document.querySelector('#subcategories').insertAdjacentHTML('beforeend', subCategoriesLi);
});
};
populateSubCategories(subsData);
};
getSubCategoriesById(`/controllername/jsonresult/ID`, { ID: categoryId });
});
});
};
liBtnClick(categoriesLiList);
};
getSubCategories();
我真是个白痴哈哈
我的api工作不正常,因此为了将来的目的,请始终使用postman测试您的api:)
另外,也不需要post请求!只是一个正常的获取请求:
await fetch(`/controllerName/JsonResult/${categoryId}`);
await fetch(`/controllerName/JsonResult/${categoryId}`);
听起来你应该做一个get请求。很抱歉,我是新手,所以我可以用get请求发送一个值??尝试将参数名称从“data”更改为ID。你可以在get请求中传递一个参数,例如ID。Post请求通常在get请求检索数据时向服务器发送数据。