代码之家  ›  专栏  ›  技术社区  ›  naspinski

415带多部分FormData的fetch POST

  •  0
  • naspinski  · 技术社区  · 4 年前

    我已经看了无数的帖子,我只是被这个应该是如此简单的问题所困扰。

    let formData = new FormData();
    formData.append('message', payload.message);
    formData.append('attachment', this.state.attachment);
    let options = {
        method: 'post',
        body: formData
    }
    fetch('api/contact', options);
    

    Request URL: https://localhost:44348/api/contact
    Request Method: POST
    Status Code: 415 
    Remote Address: [::1]:44348
    Referrer Policy: no-referrer-when-downgrade
    

    我知道不要设置我的内容类型,甚至是标题,一切看起来都很好,包括边界:

    请求标头

    :authority: localhost:44348
    :method: POST
    :path: /api/contact
    :scheme: https
    accept: */*
    accept-encoding: gzip, deflate, br
    accept-language: en-US,en;q=0.9
    cache-control: no-cache
    content-length: 10269
    content-type: multipart/form-data; boundary=----WebKitFormBoundary5iTqTV6AaKla3BMA
    origin: https://localhost:44348
    pragma: no-cache
    referer: https://localhost:44348/contact
    sec-fetch-dest: empty
    sec-fetch-mode: cors
    sec-fetch-site: same-origin
    user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36
    

    表单数据

    message: asdasd
    attachment: (binary)
    

    这是我以前在jQuery站点上使用的同一个API,它工作得很好。有什么想法吗?

    0 回复  |  直到 4 年前
        1
  •  2
  •   Anglesvar    4 年前
    let options = {
        method: 'post',
        body: formData
    }
    

    分号 ; 之后 body 在JSON声明中可能会导致运行错误。这可能是个问题,请检查一下。在附加附件时,请检查它是否正确转换为二进制格式。希望有帮助!!编码快乐!!

        2
  •  0
  •   naspinski    4 年前

    推荐文章