2

I'm building a search form where you can search for a city/country. I'm getting a response, but it adds localhost to the url

http://localhost:3000/api.geonames.org/searchJSON?q=london&username=username

Which it shouldn't do... What I'm I doing wrong?

state = {
            text: ""
          }



 handleChange = (e) => {
    this.setState({
      [e.target.name]: e.target.value
    })
  }



componentDidMount() {
    const endpoint = `${api}q=${this.state.text}&username=${userName}`
    console.log(endpoint)
    fetch(endpoint)
    .then(res => {
      console.log(res)
    })
  }



handleSubmit = (e) => {
    e.preventDefault()
    this.setState({text: ""})

    this.componentDidMount()

  }



 render() {
    return (
      <div>
        <h1>CityPop</h1>
        <form onSubmit={this.handleSubmit}>
          <h3>Search by city</h3>
          <input
            type="search"
            name="text"
            value={this.state.text}
            onChange={this.handleChange}
          />
          <button>Search city</button>
        </form>
      </div>
    )
  }
2
  • You don't show how api is set. Commented Nov 20, 2019 at 13:24
  • I'm getting it from another file where I declared the api url to a variable Commented Nov 20, 2019 at 13:27

1 Answer 1

2

Just add http/https protocol before the link:

const endpoint = `https://${api}q=${this.state.text}&username=${userName}`;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.