GET with query string with Fetch in React Native

前端 未结 7 1702
情话喂你
情话喂你 2020-12-29 01:51

I am making a request like this:

fetch(\"https://api.parse.com/1/users\", {
  method: \"GET\",
  headers: headers,   
  body: body
})

How d

7条回答
  •  一生所求
    2020-12-29 02:20

    Your first thought was right: just add them to the URL.

    Remember you can use template strings (backticks) to simplify putting variables into the query.

    const data = {foo:1, bar:2};
    
    fetch(`https://api.parse.com/1/users?foo=${encodeURIComponent(data.foo)}&bar=${encodeURIComponent(data.bar)}`, {
      method: "GET",
      headers: headers,   
    })
    

提交回复
热议问题