Reactjs Axios post请求返回401未经授权的错误
我正在开发一个登录屏幕。我正在使用axios调用api。Axios post请求在使用时返回401错误。但当我硬编码要发送的数据时,同样的代码也可以正常工作。它不会读取表单中存储的json格式数据。Reactjs Axios post请求返回401未经授权的错误,reactjs,axios,Reactjs,Axios,我正在开发一个登录屏幕。我正在使用axios调用api。Axios post请求在使用时返回401错误。但当我硬编码要发送的数据时,同样的代码也可以正常工作。它不会读取表单中存储的json格式数据。 我能做什么 class Login extends React.Component { handleSubmit = e => { e.preventDefault(); this.props.form.validateFields((err, va
我能做什么
class Login extends React.Component {
handleSubmit = e => {
e.preventDefault();
this.props.form.validateFields((err, values) => {
if (!err) {
let cred= JSON.stringify(values);
console.log('json values of form: ',cred );
axios.post('http://10.42.0.108:8000/user/login/',cred)
.then(function (response) {
console.log(response);
});
// .catch(function (error) {
// console.log(error);
// });
}
});
};
render() {
const { getFieldDecorator } = this.props.form;
return (
<React.Fragment>
<div className='login'>
<div className="login-container shadow-lg p-3 mb-5 ml-3 bg-white rounded">
<Form onSubmit={this.handleSubmit} className="login-form px-5 py-5">
<div className="text-center">
<h3 className="dark-grey-text mb-5">
<strong>Sign in</strong>
</h3>
</div>
<Form.Item>
{getFieldDecorator('outlook_id', {
rules: [{ required: true, message: 'Please input your username!' }],
})(
<Input
prefix={<Icon type="user" style={{ color: 'rgba(0,0,0,.25)' }} />}
placeholder="Username" style={{width:'300px'}}
/>,
)}
</Form.Item>
<Form.Item>
{getFieldDecorator('password', {
rules: [{ required: true, message: 'Please input your Password!' }],
})(
<Input
prefix={<Icon type="lock" style={{ color: 'rgba(0,0,0,.25)' }} />}
type="password"
placeholder="Password" style={{width:'300px'}}
/>,
)}
</Form.Item>
<Form.Item>
{/* {getFieldDecorator('remember', {
valuePropName: 'checked',
initialValue: true,
})(<Checkbox>Remember me</Checkbox>)} */}
<Button type="primary" htmlType="submit" className="login-form-button">
Log in
</Button>
</Form.Item>
</Form>
</div>
</div>
</React.Fragment>
);
}
}
const WrappedLogin = Form.create({ name: 'normal_login' })(Login);
export default WrappedLogin;
对于使用axios发布数据,不需要将数据转换为字符串,只需将其发送出去即可。正确的语句应该是
axios.post('http://10.42.0.108:8000/user/login/,值)
而不是
axios.post('http://10.42.0.108:8000/user/login/',cred)
----纳维德回答。用户:3744479将json字符串化的原因是什么?您的后端是否接收字符串或json?如果需要json,只需发送原始对象。后端需要json中的数据在handleSubmit中打印值时会看到什么?值是否与硬编码的值相同?@ArunKAnil,但数据正在
handleSubmit->let cred=JSON.stringify(值)中转换为字符串
@ArunKAnil用于使用axios发布数据不需要将数据转换为字符串,只需将其发送出去即可。正确的语句应该是axios.post('http://10.42.0.108:8000/user/login/,值)
而不是axios.post('http://10.42.0.108:8000/user/login/”,cred)
axios.post('http://10.42.0.108:8000/user/login/',cred)