admin管理员组文章数量:1415484
When clicking the button on my front end (relevant portion shown below)
async function getSample() {
const res = await fetch('/lookup/url');
const data = await res.text();
console.log(data);
}
document.getElementById('button').addEventListener('click', getSample);
async function getSample() {
fetch('http://localhost:3000/lookup/url')
.then(response => response.text())
.then(data => console.log(data));
}
I am getting this error in the terminal of my node server:
TypeError [ERR_INVALID_URL]: Invalid URL: url
and this in my console:
Fetch failed loading: GET "http://localhost:3000/lookup/url"
Can anyone provide some advice as to what I may be doing wrong and how I could fix?
Not that this matters, but backend is express
Adjustment Re: Comments
async function getSample() {
const res = await fetch('/lookup/url');
const data = await res.text();
console.log(data);
document.getElementById('button').addEventListener
('click', getSample);
fetch('http://localhost:3000/lookup/url')
.then(response => response.text())
.then(data => console.log(data));
}
When clicking the button on my front end (relevant portion shown below)
async function getSample() {
const res = await fetch('/lookup/url');
const data = await res.text();
console.log(data);
}
document.getElementById('button').addEventListener('click', getSample);
async function getSample() {
fetch('http://localhost:3000/lookup/url')
.then(response => response.text())
.then(data => console.log(data));
}
I am getting this error in the terminal of my node server:
TypeError [ERR_INVALID_URL]: Invalid URL: url
and this in my console:
Fetch failed loading: GET "http://localhost:3000/lookup/url"
Can anyone provide some advice as to what I may be doing wrong and how I could fix?
Not that this matters, but backend is express
Adjustment Re: Comments
async function getSample() {
const res = await fetch('/lookup/url');
const data = await res.text();
console.log(data);
document.getElementById('button').addEventListener
('click', getSample);
fetch('http://localhost:3000/lookup/url')
.then(response => response.text())
.then(data => console.log(data));
}
Share
Improve this question
edited Jan 7, 2021 at 1:38
Max.California
asked Jan 7, 2021 at 0:22
Max.CaliforniaMax.California
2151 gold badge2 silver badges9 bronze badges
22
-
I think it might be a CORS issue. The frontend isn't in
localhost:3000
, right? – sebasaenz Commented Jan 7, 2021 at 0:30 - @sebasaenz the front end is showing when I access localhost:3000. – Max.California Commented Jan 7, 2021 at 0:44
-
Why do you have two definitions of
getSample()
? – Barmar Commented Jan 7, 2021 at 0:46 - @Barmar Don't I need to define it, and then use it? – Max.California Commented Jan 7, 2021 at 0:47
- Are you declaring the same function twice? You don't need to do it, just once is enough – luckongas Commented Jan 7, 2021 at 0:53
1 Answer
Reset to default 1Get rid of the second definition of getSample()
. You only need one definition of the function, and it's better to use a relative URL to access resources on the same server as the front end.
async function getSample() {
const res = await fetch('/lookup/url');
const data = await res.text();
console.log(data);
}
document.getElementById('button').addEventListener('click', getSample);
本文标签: javascriptInvalid URLTypeErrorStack Overflow
版权声明:本文标题:javascript - Invalid URL | TypeError - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745170933a2645967.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论