JacobLinCool / JacobLinCool/fetch-impl

TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation

Open
#20 0 comments 1 reaction 1 assignee Assigned to @Copilot View on GitHub
Dominant language
TypeScript
Stars
2
Forks
1
PR merge metrics
No merged PRs in 30d

Description

> The error "TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation" typically occurs in JavaScript when the fetch function is called with an incorrect this context. The fetch function, as a method of the Window object, expects this to refer to the Window object when it's invoked. If fetch is extracted from window and called in a different context (e.g., as a method of a custom object or without a specific this binding), this error can arise.

When testing [leetcode-query](https://github.com/JacobLinCool/LeetCode-Query) in browser(in fact, it can't work because of CORS), I got above error and searched in Google. Seems it's better to bind globalThis in fetcher/src/fetcher.ts, or you need set custom fetcher like,
```
fetcher.set(async (...args) => {
const res = await window.fetch(...args); // call with right this context
return new Response(res.body, res);
})
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.