亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

上傳文件reactjs err 400

上傳文件reactjs err 400

守候你守候我 2022-06-09 11:21:11
當我嘗試上傳文件時出現此錯誤未捕獲(承諾中)錯誤:請求失敗,狀態碼為 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。先感謝您。
查看完整描述

2 回答

?
慕尼黑的夜晚無繁華

TA貢獻1864條經驗 獲得超6個贊

我不認為你的代碼有問題。它給出了 404 的錯誤,這意味著沒有您調用的 API。



查看完整回答
反對 回復 2022-06-09
?
守著一只汪

TA貢獻1872條經驗 獲得超4個贊

嘗試這樣的事情


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;


查看完整回答
反對 回復 2022-06-09
  • 2 回答
  • 0 關注
  • 261 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號