react使用fetch向后端传值

前端

function deleteTool () {
  var url = 'http://localhost:8080/admin/deleteTool';//传值的地址
  var data = { toolnum: '0' };//传值的内容,键:值格式
  fetch(url, {
    method: 'post',//post方法
    headers: {
      "Content-Type": "application/json;charset=utf-8",
      'Token': localStorage.getItem('token')
  },
    body: JSON.stringify(data)//转为json格式
  }).then(res => res.json())
    .catch(error => console.error('Error:', error))
    .then(response => console.log('Success:', response));
    console.log('--------'+data);
}

后端

 @RequestMapping(value="/admin/deleteTool",method = RequestMethod.POST,produces = "application/json")
    public int toDeleteTool(@RequestBody String toolnum) throws JSONException {
        JSONObject json =new JSONObject(toolnum);//解析接受到的json数据
        System.out.println("..........."+json.getString("toolnum"));
        return toolDaoImpl.deleteTool(json.getString("toolnum"));
    }
  • 必须在参数toolnum前加上@RequestBody,不然接受到的值为null。
  • json.getString(“toolnum”)为获取json中的值,详情参考java json数据取值
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React 中,子组件向父组件传值的常用方式是通过回调函数。下面是一个简单的示例,演示了子组件向父组件传递值的过程: 父组件: ```jsx import React, { useState } from 'react'; import ChildComponent from './ChildComponent'; const ParentComponent = () => { const [valueFromChild, setValueFromChild] = useState(''); const handleChildValue = (value) => { setValueFromChild(value); }; return ( <div> <h2>Value from child: {valueFromChild}</h2> <ChildComponent onChildValue={handleChildValue} /> </div> ); }; export default ParentComponent; ``` 子组件: ```jsx import React, { useState } from 'react'; const ChildComponent = ({ onChildValue }) => { const [inputValue, setInputValue] = useState(''); const handleChange = (event) => { setInputValue(event.target.value); }; const handleButtonClick = () => { onChildValue(inputValue); setInputValue(''); }; return ( <div> <input type="text" value={inputValue} onChange={handleChange} /> <button onClick={handleButtonClick}>Submit</button> </div> ); }; export default ChildComponent; ``` 在这个示例中,父组件包含一个状态 `valueFromChild`,子组件包含一个输入框和一个按钮。当用户在子组件的输入框中输入文本并点击按钮时,子组件会调用父组件传递的回调函数 `onChildValue`,将输入的值作为参数传递给父组件。父组件接收到这个值后,更新自己的状态 `valueFromChild`,并将其展示在页面上。 通过这种方式,子组件可以将数据传递给父组件,实现了子向父的值传递。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值