问题描述
我正在尝试使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 => {//您的代码..})