我创建了一个带有节点后端的小项目,并通过使用Axios库的REST调用来反馈前端来获取数据.但是,当我用它传递标题时,我不断收到错误说Failed to load resource: the server responded with a status of 401 (Unauthorized).我发现了两种方法,但都没有用.
他们是
export const getUsersDetails=()=>{
console.log('calling');
return (dispatch) => {
return axios.get('http://localhost:3030/users',{headers: { "Authorization": localStorage.getItem('jwtToken') }}).then((data)=>{
console.log('data comming',data);
dispatch(getUsersData(data));
}).catch((error)=>{
console.log('error comming',error);
dispatch(errorgetUsersData(error));
});
};
}
Run Code Online (Sandbox Code Playgroud)
和
axios.defaults.headers.common['Authorization'] = localStorage.getItem('jwtToken');
Run Code Online (Sandbox Code Playgroud)
但是当我使用邮递员时,我从后端获得所需的数据.我一直得到这个未经授权的错误的任何特殊原因?
我一直试图从一个input字段中获取输入并使用refs(通常的方式react),但它似乎没有工作.在input我得到的undefined.这是我的代码:
sendMessage = () => {
console.log(this.inputtext.value);
}
render(){
return(
<div>
<Input ref={input => this.inputtext = input;} placeholder='message'/>
<Button onClick={this.sendMessage}>Send</Button>
</div>
);
}
Run Code Online (Sandbox Code Playgroud)
我需要从click event按钮中获取输入.我无法弄清楚出了什么问题.我怎样才能input正确获得价值?