Javascript Axios获取请求并运行web服务器
我试图在web服务器运行时让Axios get请求工作,但每当我使用以下代码时:Javascript Axios获取请求并运行web服务器,javascript,get,axios,Javascript,Get,Axios,我试图在web服务器运行时让Axios get请求工作,但每当我使用以下代码时: axios.post('http://localhost:8020/posts', { title: title.value, content: content.value }) .then(function (response) { console.log(response); }) .catc
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.post('http://localhost:8020/posts', {
标题:title.value,
内容:content.value
})
.然后(功能(响应){
控制台日志(响应);
})
.catch(函数(错误){
console.log(错误);
});
displayPost();
}
异步函数displayPost(){
控制台日志(“单击”);
等待axios。获取更多信息http://localhost:8020/posts')
.try(函数(响应){
控制台日志(响应);
postContainer.innerHTML=“”;
for(设i=0;i
${response.data[i].content}`;
}
控制台日志(响应);
})
我在控制台中收到一条错误消息,说axios.get(…).try在displayPost和HTMLInputElement.deletePost中不是一个函数。有没有办法解决这个问题?因为没有
try
函数,我想它一定是axios.get()。然后()
。如果使用此选项,则不需要在开始时使用wait
。如果使用wait
,则必须是const resposnse=wait axios.get()
get('url')。然后(res=>{//您的代码..})
Axios没有
try
方法。打字错误:您的意思可能是then
,就像前面的代码块一样。