Javascript TypeError:无效的URL
当我在函数中添加带引号的常规链接时,一切正常,但当我从前面请求链接时,会出现类型错误 我试过Javascript TypeError:无效的URL,javascript,node.js,Javascript,Node.js,当我在函数中添加带引号的常规链接时,一切正常,但当我从前面请求链接时,会出现类型错误 我试过 JSON.stringify const baseURL = new URL(url1) const newURL = baseURL.href 看穿 url预览应该通过将输入框中的url发送到后端来显示。 url应该从前端到后端进行往返,呈现图像,描述…您使用的模板文字错误。模板文本是字符串,当您想使用变量时,不必将其括在引号中 i、 例如,在您的示例中,google.com变为“google.co
JSON.stringify
const baseURL = new URL(url1)
const newURL = baseURL.href
看穿
url预览应该通过将输入框中的url发送到后端来显示。
url应该从前端到后端进行往返,呈现图像,描述…您使用的模板文字错误。模板文本是字符串,当您想使用变量时,不必将其括在引号中 i、 例如,在您的示例中,google.com变为“google.com”,请注意额外的引号 会是,
let it = await grabity.grabIt(`${url1}`);
//or
let it = await grabity.grabIt(url1)
在使用模板文字时,您不需要使用引号,它应该是`${url1}`为什么首先要使用模板文字?如果您能够使用If url1==检查参数,则不应使用grabity.grabIturl1;你已经玩过这个把戏了吗?如果这个方法真的需要在值中加上引号,那就太奇怪了。
// front end
import React, {Component} from 'react';
import './App.css';
import MicrolinkCard from '@microlink/react';
class App extends Component {
constructor(props) {
super(props);
this.state = {
practiceText: "",
description: "",
value: "",
image: ""
};
}
grabityAPI() {
fetch("http://localhost:9000/grabityLink")
.then(res => res.json())
.then(res => this.setState({ title: res.title, image: res.image, desc: res.description, favi: res.favicon }))
}
callAPI() {
fetch("http://localhost:9000/testAPI")
.then(res => res.json())
.then(res => this.setState({ practiceText: res }));
}
componentWillMount() {
this.callAPI();
this.grabityAPI();
}
handleChange = async (e) => {
e.preventDefault();
this.setState({value: this.refs.value.value})
const data = { value: this.state.value}
const options = {
method: 'POST',
headers: { "Content-Type": "application/json; charset=utf-8" },
body:
JSON.stringify(data),
};
await fetch("http://localhost:9000/grabityLink", options)
}
let it = await grabity.grabIt(`${url1}`);
//or
let it = await grabity.grabIt(url1)