简体   繁体   English

如何在 FETCH PUT API 中将 json 主体作为字符串传递给 React js

[英]how to pass json body as String in FETCH PUT API in react js

how to pass json body as String in FETCH PUT API in react js?如何在 FETCH PUT API 中将 json 主体作为字符串传递给 React js? I am trying to 20 addition to the Balance like this Balance: details[0].Balance.toString() + 20 which didnt work.我正在尝试将 20 添加到 Balance 这样的 Balance: details[0].Balance.toString() + 20 中,但没有用。 i want to hve it as string altogether我想把它完全当作字符串

Any help would be greatly appreciated.任何帮助将不胜感激。 please

Below Sample code:下面的示例代码:

fetch(memberput, {
  method: 'PUT',
  headers: curHeaders,
  body: JSON.stringify({
  ID:details[0].ID.toString(),
  Balance: details[0].Balance.toString() + 20
  })
  })
  .then(function(response){
    console.log('Final ', response.json());
  })

Expectedoutput: If currentbalance is 30, it should give me 30+20 = 50预期输出:如果 currentbalance 是 30,它应该给我 30+20 = 50

Actual output: If currentbalance is 30, its giving me 3020 now.实际 output:如果 currentbalance 是 30,它现在给我 3020。

String + string = concatenate strings String + string = 连接字符串

String + number = concatenate string and number字符串 + 数字 = 连接字符串和数字

Number + number = sum of the number数+数=数之和

What you should do is something like你应该做的是像

Const balance = (Number(details[0].Balance) + 20).toString();

First, you need to ensure that the balance is a number, then add the 20 or the variable you want, and then convert it to a string.首先,你需要确保余额是一个数字,然后加上 20 或者你想要的变量,然后把它转换成一个字符串。 Then you can use that balance constant to send the data.然后您可以使用该余额常量发送数据。

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

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