如何解决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 => {//您的代码..})
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。