简体   繁体   中英

How to make a post request with async fetch in mvc and JavaScript?

Im trying to fetch subcategories from my mvc application with reference to the category id with async fetch

I already fetched the categories and its all working

but when i try to fetch the subcategories with a post request it doesn't work!

//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();

The result should be the data from the api but its not reading the ID param. what should i change in my post request??


EDIT: I am such an idiot lol my api wasn't working correctly, so for future purposes always test your apis with postman :)

also, there's no need for a post request! just a normal fetch get reques:

await fetch(`/controllerName/JsonResult/${categoryId}`);

I am such an idiot lol my api wasn't working correctly, so for future purposes always test your apis with postman :)

also, there's no need for a post request! just a normal fetch get reques:

await fetch(`/controllerName/JsonResult/${categoryId}`);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM