Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何使用axios更改json数据中的单个值_Reactjs - Fatal编程技术网

Reactjs 如何使用axios更改json数据中的单个值

Reactjs 如何使用axios更改json数据中的单个值,reactjs,Reactjs,当我想用“更改密码”菜单更新密码时出现问题。当我使用Axios.put时,数据仍然没有改变,我已经尝试过搜索,但没有人发现我的问题。我只需要知道如何使用Axios put更改单个值,就像更改密码一样 changepassword.js import React,{Component,Fragment}来自'React'; 从“react redux”导入{connect} 从“历史”导入{createHashHistory}; 从“axios”导入axios; 进口{ 卡片 万向节, 名片夹,

当我想用“更改密码”菜单更新密码时出现问题。当我使用Axios.put时,数据仍然没有改变,我已经尝试过搜索,但没有人发现我的问题。我只需要知道如何使用Axios put更改单个值,就像更改密码一样

changepassword.js

import React,{Component,Fragment}来自'React';
从“react redux”导入{connect}
从“历史”导入{createHashHistory};
从“axios”导入axios;
进口{
卡片
万向节,
名片夹,
FormGroup,
形式,
标签,
输入,
集装箱,
按钮
一行
上校,
警觉的
}从“反应带”;
从“components/Headers/UserHeader.js”导入UserHeader;
const customHistory=createHashHistory();
类SetPass扩展组件{
建造师(道具){
超级(道具);
const token=localStorage.getItem(“token”);
const email=localStorage.getItem(“电子邮件”);
const password=localStorage.getItem(“密码”);
让isLogedIn=true
如果(令牌===null | |电子邮件===null | |密码===null){
isLogedIn=false
}
此.state={
职位:[],
表格数据:{
id:“”,
名字:'',
姓氏:“”,
电子邮件:“”,
性别:'',
密码:“”,
确认:“”
},
伊斯洛格丁
}
this.handleForm=this.handleForm.bind(this);
this.submit=this.submit.bind(this);
}
getPostAPI=()=>{
const email=localStorage.getItem(“电子邮件”);
axios.get(`http://localhost:8001/dataadmin?email=${email}`)
。然后((res)=>{
这是我的国家({
帖子:res.data
})
})
}
putDataToAPI=()=>{
const email=localStorage.getItem(“电子邮件”);
axios.put(`http://localhost:8001/dataadmin?email=${email}/${this.state.formData.id}`{
密码:this.state.formData.password
})
。然后(响应=>{
这是我的国家({
表格数据:{
密码:“”,
确认:“”
},
})
控制台日志(响应);
})
.catch(错误=>{
console.log(错误);
});
// 
//console.log(axios.put(`http://localhost:8001/dataadmin?email=${email}`,this.state.formData.password)
//axios.put(`http://localhost:8001/dataadmin?email=${email}/`,this.state.formData.password)
//.然后(res=>{
//控制台日志(res);
//这是我的国家({
//表格数据:{
//id:“”,
//密码:“”,
//确认:“”
//     },
//   })
// })
//.catch(错误=>{
//控制台日志(err);
// })
}
手形=(e)=>{
让formDataNew={…this.state.formData};
formDataNew[e.target.name]=e.target.value;
这是我的国家({
formData:formDataNew
})
}
提交=()=>{
const{post}=this.state;
const email=localStorage.getItem(“电子邮件”);
const password=localStorage.getItem(“密码”);
if((this.state.formData['password']==”)| |(this.state.formData['confirm']==”){
警报(“Semua Data Harus Di Isi!!!”;
}否则
if(this.state.formData['password']!==this.state.formData['confirm'])){
警报(“密码tidak sama dengan确认密码”)
}否则
if(post.find(e=>`${e.email}${e.password}`===`${email}${password}`){
this.putDataToAPI();
警报(“Sukses Ganti密码”)
} 
}
组件安装(){
这个.getPostAPI();
}
render(){
if(this.state.isLogedIn===false){
customHistory.push(“/”)
} 
报税表(
更改密码
密码
确认密码
提交
)
}
}
功能选择(状态){
返回{
用户:state.users
}
}
导出默认连接(选择)(SetPass);
更新changepassword.js

putDataToAPI = () => {
    const email = localStorage.getItem("username");
    const { post } = this.state
    {
      post.map(post => (

        axios.put(`http://localhost:8001/dataadmin/${post.id}?email=${email}`, {
          ...this.post,
          password : this.state.formData.password,

          })
          .then(response => {
            this.setState ({
              formData : {
                password: '',
                confirm : ''
              },
            })
            console.log(response);
          })
          .catch(error => {
            console.log(error);
        })
      ))
    }
  }

我希望当表单单击提交时,密码的值被更改。但还是没有变化。有没有可能在没有id的情况下更改此内容?

请注意您在中的URL

putDataToAPI = () => {
  const email = localStorage.getItem("email");
  axios.put(`http://localhost:8001/dataadmin?email=${email}/${this.state.formData.id}`, {
     password : this.state.formData.password

    })
搞砸了。而不是
http://localhost:8001/dataadmin?email=${email}/${this.state.formData.id}
,应该是
http://localhost:8001/dataadmin/${this.state.formData.id}?email=${email}
另外,不要忘记添加第二个字段:

axios.put(`http://localhost:8001/dataadmin/${this.state.formData.id}?email=${email}`, {
     password : this.state.formData.password,
     confirm : this.state.formData.confirm
    })

请共享您的所有代码changepassword.js文件。这取决于您后端的期望/支持。您的意思是什么?我只是使用json数据伪造jsoon服务器。用axioslook看更多broo。此表单只需输入,不需要详细说明。如果详细的话,我们可以用这个。但如果只是形式?这就是我将电子邮件和密码发送到localstorage的原因。而且想(如果输入===localstorage.get-email,则密码将更改。我的问题是如何从表dataadmin中查询更新密码,其中email=localstorage get-email。ahahahawh您首先为什么要将密码存储在本地存储中?绝对不要!!!很抱歉,我将很快删除它。您在t上遇到了实现问题服务器。要将“PUT”与RESTful API一起使用,您必须在URL中标识要修改的资源,并在主体中传递要修改的对象部分。此外,在
putDataToAPI()
方法中,您只传递一个属性(密码)但是在表单中,需要密码和确认密码字段