java - 如何在reactJS的get请求中设置参数

标签 java reactjs rest get

也许这是一个非常新手的问题,但我花了很多时间来寻找一个好的方法来做到这一点,但我没有找到方便的答案。我正在尝试对 Rest api 进行简单调用,并且希望传递一个值,并将 GET 请求附加到字符串中。就像 url/foo 一样,其中 foo 是参数。我有一个查询变量,我想将其附加到 get 请求的 url 字符串末尾。预先感谢您。

class About extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            products: [],
            filteredItems: [],
            user: {},
            query: '' <-- query variable to be appended to the end of the get request
        };

    }



    componentDidMount() {

        fetch(`'myurl/${this.state.query}'`) <-- i want to append the variable at the end of the string ??
            .then(res => res.json())
            .then((result) => {
                    console.log(result);
                    this.setState({
                        products: result,
                        filteredItems: result
                    });
                }
            )
    }

    queryChange = (evt) => {
        this.setState({query: evt.target.value}) <-- update the variable state from an event
    }




最佳答案

去掉 'myurl/${this.state.query}' 中的多余引号 (')

关于java - 如何在reactJS的get请求中设置参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55892262/

相关文章:

java - 如何使用 Runtime.getRuntime().exec() 执行 ant 并关闭命令提示符

javascript - React/Javascript 乱序代码执行

javascript - 使用登录流程处理权限敏感操作的 React/Flux 方式

java - Android JSON 与 REST

python - Rest 客户端的元语言

java - Keytool 生成版本 3 证书

java - 执行被困在按钮内

java - 使用 java8 流构建集合

javascript - 将外部脚本嵌入到 Next.js 应用程序中

web-services - vba中的Protobuf web服务反序列化