如何在 React Native 中使用 fetch 发布表单? [英] How to post a form using fetch in react native?

查看:42
本文介绍了如何在 React Native 中使用 fetch 发布表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 react-native fetch api 发布包含名字、姓氏、电子邮件、密码和密码确认的表单.

I'm trying to post the form containing the first_name, last_name, email, password and password_confirmation using react-native fetch api.

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 控制台输出

Output in Rails console

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"}

因此,它将整个值作为字符串发布,而 rails 将字符串解析为变量.我想从 json 响应中去掉{".怎么做?

So, its posting the whole value as string and rails is parsing the string as a variable. I want to strip out the "{" from the json response. How to do it ?

推荐答案

所以如果我理解你的话,你希望它发布相同的字符串但不带花括号?

so if I understand you well, you want it to post the same string but just without the curly braces?

如果是这样,您可以将它们从字符串中剥离.

If that's the case, you can just strip them from the string.

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

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

这篇关于如何在 React Native 中使用 fetch 发布表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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