Curl工作正常,但Javascript出现错误

1zmg4dgp  于 2022-11-13  发布在  Java
关注(0)|答案(1)|浏览(112)

我正在一个postapi中工作,当我在curl中使用它时,它给予了我很好的响应,但在javascript中,它给了我访问控制允许起源的错误
下面是curl代码

curl -F "file=@test.txt" https://api.anonfiles.com/upload

js文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form >
        <input type="file" id="file">
        <input type="submit">
    </form>
</body>
<script>

const uploadElement = document.getElementById('file');

// Extract the file (for a single file, always 0 in the list)
const file = uploadElement.files[0];
const form = new FormData();
form.append('file', (file, 'test.txt'));

fetch('https://api.anonfiles.com/upload', {
    method: 'POST',
    body: form
});
</script>
</html>

所以我不明白为什么它在js中给予错误而不是curl。任何建议。代码会更有帮助

bvuwiixz

bvuwiixz1#

我试过让你蜷缩在https://curlconverter.com/javascript/
得到了这个:
您无法在浏览器中读取--form/-F的文件

相关问题