gpt4 book ai didi

javascript - axios Post请求没有React功能

转载 作者:行者123 更新时间:2023-12-02 20:51:43 28 4
gpt4 key购买 nike

我是 React 和 Axios 的新手,我想在 React 中使用 Axios 向 API 发送一组数据,这是 API 中的数据格式:

{
"company_name": "Karno",
"title": "Devops Engineer",
"internal_code": 856789,
"department": 1,
"location": 2,
"tags": [10, 11],
"benefits": "At Snapp, we like spending ...",
"description": "About Snapp..."
"requirements": "Mandatory QualificationsStrong Linux administration skills..."
}

这是我的代码,它将在按下按钮时将这些数据发送到 API(这些数据保存在 props 中并显示在此确认表单页面上),但我的发布请求似乎不起作用,我没有收到服务器的任何响应,并且我在服务器日志中没有看到任何 POST 请求

const headers = {
'content-type': 'application/json',
'Authorization': 'Token bba27954e46823f1f82ff2c89d19f5802e46fd3f'
}

export class Confirmation extends Component {
state = {
title: '',
company_name: '',
internal_code:'',
department:'',
location:'',
tags:[],
benefits:'',
description:'',
requirements:''
}

handleSubmit = event => {
event.preventDefault();

const job = {
title:this.values.Title,
company_name:this.values.Company,
internal_code:this.values.InternalCode,
department:this.values.Department.id,
location:this.values.Location.id,
tags:this.values.Tags.map(tag=>tag.id).join(', '),
benefits:this.values.Benefits,
description:this.values.Detailss,
requirements:this.values.requirements
}
axios.post('/api/jobs/job-creation/',{headers:headers}, job)
.then(res=> {
console.log(res)
console.log(res.data)
})
}

continue = e => {
e.preventDefault();
this.props.nextStep();
};

back = e => {
e.preventDefault();
this.props.prevStep();
};

render () {
const {
values: {
Title, Benefits,
Company, InternalCode, Detailss, Department,Tags, Salary,requirements,Location,newTags
}
} = this.props
return (
<form onSubmit={this.handleSubmit}>
<div className='container'>
<div className='content'>
<Container>
<Row>
<Col xs='6' sm='4' className='TextContent'>
<Row className='TextInside'> Salary: {Salary} </Row>
<Row> Location: {Location.label}</Row>
<Row> New Tags: {newTags.join(', ')} </Row>
</Col>
<Col xs='6' sm='4' className='TextContent'>
<Row> Company: {Company}</Row>
// info in the {} are shown on page and need to be sent to api
<Row> Internal Code: {InternalCode}</Row>
<Row> Pre Tags: {Tags.map(tag=>tag.label).join(", ")}</Row>
<Row> Department: {Department.label}</Row>
</Col>
<Col xs='6' sm='4' className='TextContent'>
<Row> Job Title: {Title}</Row>
<Row> Benefits: {Benefits}</Row>
</Col>
</Row>
</Container>
</div>
<div className='Detailss' style={{width:'1000px'}}>
{Detailss}
</div>
<div className='req'>
{requirements}
</div>


//buttons
<div className='buttons-container' style={{position:'relative',bottom:'20px'}}>
<button onClick={this.back} className='previous'>قبلی</button>
<button type='submit' onClick={this.continue} className='next'>ادامه</button>
</div>

</Container>
</Container>
</div>
</form>

我在这里做错了什么?

最佳答案

在触发表单的 onSubmit 处理程序之前,提交按钮上的 onClick 监听器会触发重定向。移除 onClick 监听器并将 this.continue() 移至 post 请求的 .then 回调。

此外,从 continue 方法和 e.preventDefault 中删除“e”参数,并将作业对象中的 this.values 更改为 this.props.values。

关于javascript - axios Post请求没有React功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61594011/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com