javascript - 上传文件reactjs err 400
问题描述
当我尝试上传文件时出现此错误
未捕获(承诺中)错误:请求失败,状态码为 404"。
我不明白为什么。这是我的一段代码。
import React, {Component}from 'react';
import '../css/formLocation.css';
import axios from 'axios'
class FormLocation extends Component {
constructor(props) {
super(props);
this.state = {
file : null
}
}
handleClick9 = (e) => {
e.preventDefault();
this.setState({step9 : true})
const formData = new FormData();
formData.append('images' , this.state.media)
axios.post('http://localhost:1337/testmedias/', formData)
.then((response) => {
console.log(response);
})
}
handleChangefile = e => {
this.setState({
media : e.target.files[0]
})
}
render() {
return (
<form>
<div className='form' id='input' >
<h6 className={this.state.step9? 'true': 'false'}>Ajoutez des images ?</h6>
<input type='file' name='media' className='input' onChange={this.handleChangefile}/>
<br/>
<button onClick={this.handleClick9} >Soumettre</button>
</div>
</form>
);
}
}
export default FormLocation;
我正在为 API 使用 Strapi。先感谢您。
解决方案
我不认为你的代码有问题。它给出了 404 的错误,这意味着没有您调用的 API。
推荐阅读
- ubuntu - 无法在 ubuntu 中连接端口 443 for https
- java - 无法实例化 MatchXpath
- react-native - react native 中的进度条和 axios
- beautifulsoup - 使用 GraphQL 更新后如何抓取网站
- javascript - 如何从预定义的变量中指定 Puppeteer 中的浏览器语言
- typescript - 如何在打字稿配置中排除多个文件扩展名?
- azure - 我可以将 Azure B2C 与我自己的登录表单一起使用吗?
- function - 如何在 clisp 函数中将 .png 文件作为参数传递
- php - 如何在不刷新的情况下更新 PHP 页面
- python - 如何对列表中找到的迭代最大值求和,而不会与其他小于两个索引的先前最大值发生冲突?