使用React Native中的Fetch使用查询字符串进行GET [英] GET with query string with Fetch in React Native

查看:98
本文介绍了使用React Native中的Fetch使用查询字符串进行GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在发出这样的请求:

I am making a request like this:

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

如何传递查询字符串参数?我是否只是将它们添加到URL?我在 docs 中找不到示例.

How do I pass query string parameters? Do I simply add them to the URL? I couldn't find an example in the docs.

推荐答案

您的第一个想法是正确的:只需将其添加到URL.

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,   
})

这篇关于使用React Native中的Fetch使用查询字符串进行GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆