小编Zac*_*ack的帖子

如何使用fetch api发布表单数据?

我的代码:

fetch("api/xxx", {
    body: new FormData(document.getElementById("form")),
    headers: {
        "Content-Type": "application/x-www-form-urlencoded",
        // "Content-Type": "multipart/form-data",
    },
    method: "post",
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用fetch api发布我的表单,它发送的正文如下:

-----------------------------114782935826962
Content-Disposition: form-data; name="email"

test@example.com
-----------------------------114782935826962
Content-Disposition: form-data; name="password"

pw
-----------------------------114782935826962--
Run Code Online (Sandbox Code Playgroud)

(我不知道为什么每次发送边界的数字都会改变...)

我希望用"Content-Type"发送数据:"application/x-www-form-urlencoded",我该怎么办?或者,如果我只需处理它,我如何解码控制器中的数据?


谁回答我的问题,我知道我可以做到:

fetch("api/xxx", {
    body: "email=test@example.com&password=pw",
    headers: {
        "Content-Type": "application/x-www-form-urlencoded",
    },
    method: "post",
}
Run Code Online (Sandbox Code Playgroud)

我想要的是像$("#form").jQuery中的serialize()(没有使用jQuery)或者在控制器中解码mulitpart/form-data的方法.谢谢你的回答.

javascript fetch-api

82
推荐指数
8
解决办法
8万
查看次数

A 扩展 B;类型 'Pick<A, Exclude<keyof A, keyof B>> & B' 不可分配给类型 'A'.ts(2322)

这是错误还是我误解了打字稿?

示例代码如下:

type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;

const func = <A extends B, B>() => {
  const aWithoutB: Omit<A, keyof B> = {} as any; // The {} assignment isn't important here.

  const b: B = {} as any;

  const a: A = {
    ...aWithoutB,
    ...b
  }; // warning here
};

Run Code Online (Sandbox Code Playgroud)

TypeScript 3.4.5 的 IDE (VS Code) 警告:

Type 'Pick<A, Exclude<keyof A, keyof B>> & B' is not assignable to type 'A'.ts(2322)
Run Code Online (Sandbox Code Playgroud)

我认为这两种类型是平等的,但不是。是什么原因?

typescript

3
推荐指数
1
解决办法
96
查看次数

标签 统计

fetch-api ×1

javascript ×1

typescript ×1