首页 > 解决方案 > 发布请求中的 400(错误请求),axios

问题描述

我在 postman 上向这个 url http://localhost:8000/files/发出了 post 请求 。这是成功的。但是,在我的 react 前端尝试相同的操作会产生400 (bad request error)。它还说Uncaught (in promise) Error: Request failed with status code 400

这是我的代码:

提交处理程序

 submitHandler(event) {
        event.preventDefault();
        const headers = {
        'Content-Type': 'application/json',
    }
        const data = new FormData()
        data.append('file', this.state.pdf_file)
        console.warn(this.state.pdf_file);
        axios
            .post("http://localhost:8000/files/", data, {headers: headers})
            .then(res => {
                console.warn(res);
            })

onFileChange


 onFileChange(event) {
        this.setState({
            pdf_file: event.target.files[0],
        });
    };

文件上传div

 <div>
      <input type="file" name="file" onChange={this.onFileChange}/>
 </div>

我尝试了几个没有解决方案的链接。我很乐意在这里得到帮助。

标签: react-nativeaxios

解决方案


推荐阅读