收到一个状态码为500的请求失败的AxiosError。

huangapple go评论67阅读模式
英文:

Geting an AxiosError of request failed with status code 500

问题

以下是您提供的代码的翻译部分:

import axios from 'axios'
import React, { useState } from 'react'
import { Checkbox, Icon } from '@chakra-ui/react'
import { Text, Container, VStack, FormControl, Input, Button, InputGroup, Box, Grid, GridItem } from '@chakra-ui/react'
import { AiFillDelete } from 'react-icons/ai';
import { FiEdit } from 'react-icons/fi';
import { GrCheckbox, GrCheckboxSelected } from 'react-icons/gr';
import TaskBox from '../Components/taskBox';
import { useToast } from '@chakra-ui/react';

const Task = () => {
    const [loading, setLoading] = useState(false)
    const [taskName, setTaskName] = useState('')
    const toast = useToast()
    async function handleSubmitTask() {
        try {
            const config = {
                header: {
                    "Content-type": "application/json",
                },
            }
            const { data } = await axios.post("/api/tasks", { taskName }, config)

        } catch (err) {
            setLoading(false)
            console.log(err);
        }
        setLoading(false)
    }
    return (
        <header>
            <section className="container">
                <VStack spacing={'2.5rem'} className="box-heading" borderRadius={'.3rem'}>
                    <Text fontSize={'3xl'}>Task Manager</Text>
                    <FormControl display={'flex'} flexDir={'column'} gap={'1rem'}>
                        <Input
                            type={'text'}
                            id={'task'}
                            placeholder={'Enter Task'}
                            onChange={(e) => setTaskName(e.target.value)}
                        />
                        <Button bg={'blue.500'} color={'white'} fontSize={'1rem'} justifySelf={'end'} className='addTask' onClick={handleSubmitTask} isLoading={loading}>
                            Add Task
                        </Button>
                    </FormControl>
                </VStack>
                <article className="box-list">
                    <TaskBox />
                </article>
            </section>
        </header>
    )
}
export default Task

请注意,上面的翻译只包括您提供的代码,不包括其他内容。如果您有其他需要翻译的部分,请提供相应的文本。

英文:

Hello guys I'm building a simple task manager app and while trying to post data from the front page I got an AxiosError of request failed with status code 500. However, when I make the same post request in postman it works fine.
Below is my code

import axios from &#39;axios&#39;
import React, {useState} from &#39;react&#39;
import { Checkbox, Icon } from &#39;@chakra-ui/react&#39;
import {Text, Container, VStack, FormControl, Input, Button, InputGroup, Box, Grid, GridItem } from &#39;@chakra-ui/react&#39;
import { AiFillDelete } from &#39;react-icons/ai&#39;;
import {FiEdit} from &#39;react-icons/fi&#39;
import {GrCheckbox, GrCheckboxSelected} from &#39;react-icons/gr&#39;
import TaskBox from &#39;../Components/taskBox&#39;;
import {useToast} from &#39;@chakra-ui/react&#39;
const Task = ()=&gt;{
const [loading, setLoading] = useState(false)
const [taskName, setTaskName] =useState(&#39;&#39;)
const toast = useToast()
async function handleSubmitTask(){
try {
const config = {
header: {
&quot;Content-type&quot;: &quot;application/json&quot;,
},
}
const {data} = await axios.post(&quot;/api/tasks&quot;, {taskName}, config)
} catch (err) {
setLoading(false)
console.log(err);
}
setLoading(false)
}
return(
&lt;header&gt;
&lt;section className=&quot;container&quot;&gt;
&lt;VStack spacing={&#39;2.5rem&#39;} className=&quot;box-heading&quot; borderRadius={&#39;.3rem&#39;}&gt;
&lt;Text fontSize={&#39;3xl&#39;}&gt;Task Manager&lt;/Text&gt;
&lt;FormControl display={&#39;flex&#39;} flexDir={&#39;column&#39;} gap={&#39;1rem&#39;}&gt;
&lt;Input 
type=&#39;text&#39;
id=&#39;task&#39;
placeholder=&#39;Enter Task&#39;
onChange={(e)=&gt; setTaskName(e.target.value)}
/&gt;
&lt;Button bg={&#39;blue.500&#39;} color={&#39;white&#39;} fontSize={&#39;1rem&#39;} justifySelf={&#39;end&#39;} className=&#39;addTask&#39; onClick={handleSubmitTask} isLoading={loading}&gt;
Add Task
&lt;/Button&gt;
&lt;/FormControl&gt;
&lt;/VStack&gt;
&lt;article className=&quot;box-list&quot;&gt;
&lt;TaskBox /&gt;
&lt;/article&gt;
&lt;/section&gt;
&lt;/header&gt;
)
}
export default Task```
</details>
# 答案1
**得分**: 1
`header` 应该使用复数形式,而 `Content-type` 应该用大写字母 `T`:
```javascript
const config = {
headers: {
"Content-Type": "application/json",
},
}
英文:

header should be in plural form and Content-type should be with a capital T:

const config = {
headers: {
&quot;Content-Type&quot;: &quot;application/json&quot;,
},
}
</details>

huangapple
  • 本文由 发表于 2023年6月29日 20:21:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581017.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定