最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to post a form using fetch in react native? - Stack Overflow

programmeradmin3浏览0评论

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

Output in Rails console

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

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 ?

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

Output in Rails console

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

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 ?

Share Improve this question edited Jun 5, 2015 at 9:39 Roemer 3,5761 gold badge18 silver badges32 bronze badges asked Jun 5, 2015 at 9:17 Piyush ChauhanPiyush Chauhan 1,5432 gold badges25 silver badges38 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

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

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.`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    body: requestBody
})
发布评论

评论列表(0)

  1. 暂无评论