React Native - 与 API 交互 - 导致问题的特殊字符 - 最好的方法是什么?

React Native - Interact with API - Special Characters Causing Issues - What's the best way to do this?

我无法始终如一地成功发送 may/may 不包含特殊字符的表单变量,例如? & #

根据我尝试转义字符的位置,我在读取数据服务器端时会遇到不同的错误。

我知道 React Native 0.7 应该更新以包含 formdata 但想知道我是否可以安全地 post 对象而不需要它。

有人已经 post 解决了类似的问题,但没有示例代码 post 来说明 POST 的工作原理:

我已经尝试过 - 除其他外:

fetch(APIURL, {
  method: 'POST',
  body: JSON.stringify({
    object1: {
      param1a: "value 1a",
      param1b: "value 1b - with bad chars & # ?",
    },
    object2:
    {
      param2a: "value 2a",
      param2b: 0,
    }
  })
})

但它将数据分组到一个未命名的参数中(更改 API 以接受这不是一个选项)。

还有这个:

fetch(APIURL, {
  method: 'GET',
  accessPackage: JSON.stringify({
      accessToken: "abc123",
      tokenType: 2,
  }),
  taggData: JSON.stringify({
    title: "test",
    wishlistID: 0,
    anotherVar: "anotherVal"
  })
})

我希望以两个字符串的形式接收数据,可以在另一端将其解析为 json 个对象。

查看 fetch repo https://github.com/github/fetch 没有帮助,因为这假设 post 是完整的 JSON post(事实并非如此)或使用 React Native 尚不可用的 FormData。

另一种解决方案可能是将所有数据安全地 encode/serialise 到 URL 参数,但到目前为止这也被证明是不一致的,尤其是 # 字符。

最好的方法是什么?

"it groups the data into a single unnamed parameter (changing the API to accept this is not an option)."

会的,因为您已经设置了 post 正文。这就是它应该如何工作的。

I wish to receive the data as two strings that can be parsed as json objects at the other end.

你可以做任何你想做的事,但这里没有魔法。您将收到一个字符串,即您设置 body 的字符串。同样,post 正文可以包含任何内容,但不应与 "special" 字符混淆。很可能是您的服务器端导致了问题。

如果你想使用 FormData,那么我认为它会在 v0.7.0 中,现在应该随时发布,或者你可能只将 JS 文件包含在你自己的项目中。你可以找到 it here. Usage examples are in the UIExplorer demo.