Axios GET请求和正在运行的Web服务器

问题描述

我正在尝试使Axios GET请求在Web服务器运行时正常工作,但是每当我使用此代码时:

axios.post('http://localhost:8020/posts',{
        title: title.value,content: content.value
        })
        .then(function (response) {
            console.log(response);
        })
        .catch(function (error) {
            console.log(error);
    });
    displayPost();
} 

async function displayPost() {
    console.log("click");
    await axios.get('http://localhost:8020/posts')
        .try(function (response){
            console.log(response);
            postContainer.innerHTML = "";
            for(let i = 0; i < response.data.length; i++) {
                postContainer.innerHTML += `<p>${response.data[i].title}</p>
                                            <p>${response.data[i].content}</p>`;
            }
            console.log(response);
        })

我在控制台中收到一条错误消息,说axios.get(...)。try不是displayPost和HTMLInputElement.deletePost上的函数。有办法解决这个问题吗?

解决方法

因为没有try函数,所以我认为它必须是axios.get().then()。如果使用此选项,则一开始就不需要await。而且,如果您使用await,则必须为const resposnse = await axios.get()

,

axios.get('url')。then(res => {//您的代码..})

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...