[英]Returning multiple errors in console 30 seconds after form submit
總而言之,我的表單結果基於提交的 URL,當提交表單時,我正在從我的快遞后端獲取數據。
當我搜索 url 時,我的控制台中似乎沒有發生任何事情。 然后,大約 30 秒后,我收到控制台錯誤
GET https://localhost.com/lookup/url net::ERR_CONNECTION_TIMED_OUT
指向const response = await fetch(url, {
and getData ('https://localhost.com/lookup/url')
的行以及以下錯誤:
Uncaught (in promise) TypeError: Failed to fetch
Promise.then (async)
(anonymous)
指向.then(data => {
最終導致fetch failed loading: 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")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.