简体   繁体   English

如何使用fetch in native native发布表单?

[英]How to post a form using fetch in react native?

I'm trying to post the form containing the first_name, last_name, email, password and password_confirmation using react-native fetch api. 我正在尝试使用react-native fetch api发布包含first_name,last_name,email,password和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,
  })
})

Output in Rails console 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"}

So, its posting the whole value as string and rails is parsing the string as a variable. 因此,它将整个值作为字符串和rails发布,将字符串解析为变量。 I want to strip out the "{" from the json response. 我想从json响应中删除“{”。 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,“”)

so that would look as follows 所以看起来如下

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM