如何 post 在 React Native 中使用 fetch 的表单?

How to post a form using fetch in react native?

我正在尝试 post 使用 react-native fetch api 包含 first_name、last_name、电子邮件、密码和 password_confirmation 的表单.

fetch('http://localhost:3000/auth', {
  method: 'post',
  body: JSON.stringify({
    config_name: 'default',
    first_name: this.state.first_name,
    last_name: this.state.last_name,
    email: this.state.email,
    password: this.state.password,
    password_confirmation: this.state.password_confirmation,
  })
})

在Rails控制台中输出

Parameters: {"{\"config_name\":\"default\",\"first_name\":\"Piyush\",\"last_name\":\"Chauhan\",\"email\":\"piyushchauhan2011@gmail.com\",\"password\":\"diehard4\",\"password_confirmation\":\"diehard4\"}"=>"[FILTERED]"}
Unpermitted parameter: {"config_name":"default","first_name":"Piyush","last_name":"Chauhan","email":"piyushchauhan2011@gmail.com","password":"diehard4","password_confirmation":"diehard4"}

因此,它 post 将整个值作为字符串,而 rails 正在将字符串解析为变量。我想从 json 响应中删除“{”。怎么做?

所以如果我理解你的话,你想要它 post 相同的字符串但只是没有花括号?

如果是这种情况,您可以将它们从字符串中删除。

.replace(/{|}/gi, "")

所以看起来如下

fetch('http://localhost:3000/auth', {
method: 'post',
body: JSON.stringify({
  config_name: 'default',
  first_name: this.state.first_name,
  last_name: this.state.last_name,
  email: this.state.email,
  password: this.state.password,
  password_confirmation: this.state.password_confirmation,
  }).replace(/{|}/gi, "")
})
const m = encodeURIComponent(userEmail);
const p = encodeURIComponent(userPassword);
const requestBody = `email=${m}&pass=${p}`;
fetch(`http://server.com`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    body: requestBody
})