0

What's the deal with this one.. this gives me the response https://hn.algolia.com/api/v1/search?query=Latest&page=0

fetchSearchTopStories(searchTerm, page = 0){
      fetch(`${PATH_BASE}${PATH_SEARCH}?${PARAM_SEARCH}${searchTerm}&${PARAM_PAGE}\
${page}`)
      .then(response => response.json())
      .then(result => this.setSearchTopStories(result))
      .catch(e => {

      })
    }

but when i do this.. it gives me GET https://hn.algolia.com/api/v1/search?query=Latest&page=${page} and a No 'Access-Control-Allow-Origin' error

 fetchSearchTopStories(searchTerm, page = 0){
      fetch(`${PATH_BASE}${PATH_SEARCH}?${PARAM_SEARCH}${searchTerm}&${PARAM_PAGE}\${page}`)
      .then(response => response.json())
      .then(result => this.setSearchTopStories(result))
      .catch(e => {

      })
    }

it's the same code.. the second one ${page} is not in new line the second one gives me the error above.

Zoe
  • 27,060
  • 21
  • 118
  • 148
null
  • 1,062
  • 1
  • 9
  • 21

1 Answers1

0

Try

 fetchSearchTopStories(searchTerm, page = 0){
      fetch(`${PATH_BASE}${PATH_SEARCH}?${PARAM_SEARCH}${searchTerm}&${PARAM_PAGE}`+encodeURIComponent(`\${page}`))
      .then(response => response.json())
      .then(result => this.setSearchTopStories(result))
      .catch(e => {

      })
    }

Might be an encoding problem with the backslash.

You can see the URL forms correctly here: http://jsfiddle.net/e5fqb5vx/6/

Aaron Franco
  • 1,560
  • 1
  • 14
  • 19