Javascript 表单提交30秒后在控制台返回多个错误
总之,我的表单结果基于提交的URL,提交表单时,我从express后端获取数据 当我搜索url时,我的控制台中似乎什么也没有发生。然后,大约30秒后,我收到控制台错误Javascript 表单提交30秒后在控制台返回多个错误,javascript,Javascript,总之,我的表单结果基于提交的URL,提交表单时,我从express后端获取数据 当我搜索url时,我的控制台中似乎什么也没有发生。然后,大约30秒后,我收到控制台错误 GET https://localhost.com/lookup/url net::ERR_CONNECTION_TIMED_OUT 指向const response=wait fetch(url,{和getData('https://localhost.com/lookup/url)和以下错误: Uncaught (in pr
GET https://localhost.com/lookup/url net::ERR_CONNECTION_TIMED_OUT
指向const response=wait fetch(url,{
和getData('https://localhost.com/lookup/url)
和以下错误:
Uncaught (in promise) TypeError: Failed to fetch
Promise.then (async)
(anonymous)
指向行。然后(数据=>{
最终导致fetch加载失败:GET”https://localhost.com/lookup/url“
完整的代码块如下所示:
document.getElementById('search').addEventListener('submit', function(e) { e.preventDefault(); getData(); })
async function getData(url = '', data = {}) {
const response = await fetch(url, {
method: 'GET',
mode: 'cors',
cache: 'no-cache',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json'
},
});
return response.json();
}
getData ('https://localhost.com/lookup/url')
.then(data => {
console.log(data);
})
任何关于我会做错什么的见解,以及如何解决这一问题以便从我的后端返回json的答案都将不胜感激。仍在学习中,请立即为所有错误道歉
回复:关于端口#的评论,这是我如何进行端口设置的
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
```
Using the // in the url will comment out the rest
fetch(“https://localhost.com/location/url“”
似乎有点奇怪,原因有二:
localhost
,而不是localhost.com
和https
连接fetch()
无法与https://localhost.com/location/url
。因此,在默认超时30秒后,它会抛出一个错误
如果域名localhost.com
是正确的(也许这只是文章中真实域名的伪装),那么您可能错过了端口。因为https://localhost.com
默认情况下将尝试连接到端口443
所以你可能只想
getData("http://localhost:port/lookup/url")
或者,如果您的服务器同时托管前端文件和api,您只需执行以下操作即可
getData("/lookup/url")
您确定localhost.com是一个网站吗?确保您托管的服务器已正确链接您可能需要的
http://localhost:3000
,将3000
替换为Express服务器正在侦听的任何端口。@ZacAnger更新了原始端口post@DaMahdi03后端的My app.get是app.get('/lookup/:url'
那么前端是否应该是lookup/url?如果您的express server也在同一端口上为您的前端提供服务(使用express.static或使用带res.render的模板),那么是的,您可以完全省略方案、域和端口。如果您的前端位于不同的端口,则需要将它们保留在