如何将在textinput中输入的值传递到url



嘿,伙计们,我正在尝试用react native中的后端API创建搜索,我必须将输入到TextInput中的单词传递到url。我不确定我做得是否正确,任何机构都能帮助我纠正吗

这是代码。

this.state = {
      search: "",
    }
async onSearchPressed() {
    try {
      let response = await fetch("http://www.endpoints.com/search/{this.state.search}", {
        method: "GET",
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json'
        },
      }); 

      render = () => {
    let fields = [
            {ref: 'search', placeholder: 'search', keyboardType:'default',secureTextEntry: false},];
     return (
       <TextInput
          {...fields[0]}
          onChangeText={(val) => this.setState({search: val})}
          value={this.state.search}
        />
       <TouchableOpacity onPress={this.onSearchPressed.bind(this)} />

看起来,它将{this.state.search}作为字符串。

更改

let response = await fetch("http://www.endpoints.com/search/{this.state.search}", {

let response = await fetch("http://www.endpoints.com/search/"+this.state.search, {

您尝试使用的是https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Template_literals.

应该是这样的:

fetch(`http://www.endpoints.com/search/${this.state.search}`

相关内容

  • 没有找到相关文章

最新更新