无法发出帖子请求

时间:2021-03-31 08:30:52

标签: json reactjs forms post form-submit

我需要从用户输入一个需要发送到safenote API的文件 原始卷曲命令:- curl -F "file=@password.txt" https://safenote.co/api/file -F password=secret -F lifetime=72 参数: 属性类型 必填 描述 file file yes 一次一个文件。最大文件大小 20MB Json 格式:-

{
    "url": "https://safenote.co/api/file",
    "raw_url": "https://safenote.co/api/file",
    "method": "post",
    "files": {
        "file": "password.txt"
    },
    "data": {
        "password": "secret",
        "lifetime": "72"
    }
}

我的js文件

import axios from 'axios';

export default function Upload() {
    const [selectedFile, setSelectedFile] = useState(null);
    const handleOnSubmit = e => {
        e.preventDefault();
        axios({
            method: "post",
            mode:"no-cors",
            url: "https://safenote.co/api/file",
            data: selectedFile,
            headers: { "Content-Type": "multipart/form-data",
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "GET,PUT,POST,DELETE,PATCH,OPTIONS"},
          })
            .then(function (response) {
              console.log(response);
            })
            .catch(function (response) {
              console.log(response);
            });
    }
        return(
                <form onSubmit={handleOnSubmit} encType="multipart/form-data">
                        <input type="file" name="attachment" accept="image/png, image/jpeg" 
                            onChange={(e) => setSelectedFile(e.target.files[0])}/>
                        <button type="submit">Submit Paper</button>
                </form>
        );
    }

Error snapshot of the console

0 个答案:

没有答案