簡體   English   中英

如何使用fetch in native native發布表單?

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

我正在嘗試使用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,
  })
})

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

因此,它將整個值作為字符串和rails發布,將字符串解析為變量。 我想從json響應中刪除“{”。 怎么做 ?

所以,如果我理解你,你希望它發布相同的字符串,但沒有花括號?

如果是這種情況,您可以從字符串中刪除它們。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM