Fetch TypeError:无法在“ Window”上执行“ fetch”:非法调用

Val*_*ika 4 javascript fetch node.js

我试图使用fetch进行来自反应的后端调用,而没有像Axios这样的库。

api.ts

export const sendSuggestion = ((data: any): Promise<any> => {
console.log(JSON.stringify(data));
const apiUrl = `/api/suggest/`;
return fetch(apiUrl, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(checkStatus)
    .then(r => r.json())
    .then(data => {
        console.log(data);
    });
});

const checkStatus = ((response: any) => {
  if (response.ok) {
    return response;
  } else {
    var error = new Error(response.statusText);
    console.log(error);
  //return Promise.reject(error);
  }
})
Run Code Online (Sandbox Code Playgroud)

我也包括npm模块,它是polyfill https://www.npmjs.com/package/unfetch并将其导入我的代码中

 import fetch from 'unfetch'


 console.log(fetch) returns in console:
 function fetch() { [native code] } 
Run Code Online (Sandbox Code Playgroud)

我不明白是什么问题。

Qba*_*ack 12

就我而言,我有一堂这样的课:

export class HTTPClient {
  constructor(fetch: FetchFunction = window.fetch) {
    this.fetch = fetch;
  }
  
  async performRequest(data: any) {
    return await this.fetch(data);
  }
};
Run Code Online (Sandbox Code Playgroud)

performRequest抛出此错误 ( Fetch TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation)。

我已将其更改为:

export class HTTPClient {
  constructor(
    fetch: FetchFunction = (...args) => window.fetch(...args)
  ) {
    this.fetch = fetch;
  }

  async performRequest(data: any) {
    return await this.fetch(data);
  }
};
Run Code Online (Sandbox Code Playgroud)

现在可以了。

我想问题是在第一个示例中,this参数被反弹,HTTPClient因此fetch函数没有所需的this引用。window

  • 更简单的解决方案是绑定:`globalThis.fetch.bind(globalThis)` (4认同)

Bra*_*son 7

非常具体的用例,但我在构建 Chrome 扩展程序时遇到了这个问题。

问题是我headers在请求选项中指定了一个对象列表,当它应该是一个对象时。

我正在构建以下请求:

const requestOptions = {
  method: 'POST',
  searchParams: {
    code: code,
  },
  headers: [
    {'content-type': 'application/x-www-form-urlencoded'},
  ],
};
const getAccessToken = `https://example.com/oauth2/token`;
fetch(getAccessToken, requestOptions)
  .then(r => r.text())
  .then(token => {
    console.log('token is', token);
  });
Run Code Online (Sandbox Code Playgroud)

我通过更改来修复它:

headers: [
  {'content-type': 'application/x-www-form-urlencoded'},
],
Run Code Online (Sandbox Code Playgroud)

到:

headers: {
  'content-type': 'application/x-www-form-urlencoded',
},
Run Code Online (Sandbox Code Playgroud)


Tia*_*vêa 5

使用unfetch,您可以执行以下操作:

const fetch = unfetch.bind();

如果要使用窗口:

const fetch = window.fetch.bind(window);