简体   繁体   English

通过JS Fetch API上传到Imgur API的图像被浏览器取消

[英]Image upload to Imgur API via JS Fetch API is canceled by browser

I wrote a small application on Codepen to upload to a specific Imgur album using Javascript's Fetch API. 在Codepen上编写了一个小应用程序,可以使用Javascript的Fetch API上传到特定的Imgur专辑。 The application is failing to upload, and I'm not sure why since I'm not actually getting a response from the server. 该应用程序无法上传,我不确定为什么,因为我实际上没有从服务器获得响应。 The browser appears to be canceling the request rather than sending it off. 浏览器似乎正在取消请求,而不是发送请求。

Chrome开发者工具网络标签页请求已取消

Chrome开发者工具要求详细信息

Here's my code: 这是我的代码:

const form = document.querySelector('form');
form.addEventListener('submit', event => {
  const fileInput = event.target.querySelector('input');
  const imageFile = fileInput.files[0];

  const formData = new FormData();
  formData.append('image', imageFile);
  formData.append('album', 'EVGILvAjGfArJFI');

  fetch('https://api.imgur.com/3/image', {
    method: 'POST',
    headers: new Headers({
      'Content-Type': 'multipart/form-data',
      Authorization: 'Client-ID 90ef1830bd083ba',
    }),
    body: formData
  }).then(response => {
    if (response.ok) {
      alert('Image uploaded to album');       
    }
  }).catch(error => {
    console.error(JSON.stringify(error));
    alert('Upload failed: ' + error);
  });
});

I'm not sure if I need to be reading the file or if I can pass the file object directly into the form data. 我不确定是否需要读取文件,或者是否可以将文件对象直接传递到表单数据中。 I've seen examples of both methods online, and I've tried both with the same result. 我在线上已经看到了这两种方法的示例,并且都尝试了相同的结果。 I notice dev tools doesn't seem to show the image data in the request body. 我注意到开发工具似乎并未在请求正文中显示图像数据。 Not sure if that means it isn't being sent or if Chrome is just not displaying it since it wouldn't be human-readable anyway. 不知道这是否意味着它没有被发送,或者Chrome是否只是不显示它,因为无论如何它都不是人类可读的。 I've tried the request with CORS mode on and off. 我尝试过在CORS模式下打开和关闭该请求。

Any guidance on what I'm doing wrong here? 关于我在这里做错什么的任何指导吗?

For FormData, you don't actually need the Content-Type header. 对于FormData,您实际上不需要Content-Type标头。 The browser automatically adds that for you when have a FormData type in the body. 当主体中具有FormData类型时,浏览器会自动为您添加。 I think that's because that header needs some additional info (when the browser automatically handles it, it looks like multipart/form-data; boundary=----WebKitFormBoundaryvAmR7mCXOyHiPIH5 on my machine). 我认为这是因为该标头需要一些附加信息(当浏览器自动处理它时,它看起来像multipart/form-data; boundary=----WebKitFormBoundaryvAmR7mCXOyHiPIH5在我的机器上)。

Here's the updated pen that looks like it's working: https://codepen.io/ccnokes/pen/wyBYOd 这是看起来像在工作的更新笔: https : //codepen.io/ccnokes/pen/wyBYOd

Also, because you're submitting that data via a <form> but handling the request via fetch , you need to tell the <form> not to do a full page navigation change to POST the form (which cancels the fetch). 另外,由于您是通过<form>提交数据,而是通过fetch处理请求,因此您需要告诉<form>不要对POST表单进行整页导航更改(这将取消获取)。 So I added event.preventDefault() in the submit handler on that form. 因此,我在该表单的submit处理程序中添加了event.preventDefault()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM