最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

how can i use fetch in pure javascript file? - Stack Overflow

programmeradmin2浏览0评论

I want to use fetch mand in pure javascript file. But i get an error when code execute. I use visual studio code with code runner extension. Is fetch method only use with node.js or in developer console ? How can i use fetch mand in pure javascript file ?

fetch("/?results=5000") .then(t=>t.JSON()) .then(result=>console.log(result))

Error Message :

[Running] node "c:\Users\huseyinoz\Desktop\javatest\tempCodeRunnerFile.js" c:\Users\huseyinoz\Desktop\javatest\tempCodeRunnerFile.js:2 fetch("/?results=5000") ^

ReferenceError: fetch is not defined at Object. (c:\Users\huseyinoz\Desktop\javatest\tempCodeRunnerFile.js:2:1) at Module._pile (module.js:652:30) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32)

I want to use fetch mand in pure javascript file. But i get an error when code execute. I use visual studio code with code runner extension. Is fetch method only use with node.js or in developer console ? How can i use fetch mand in pure javascript file ?

fetch("https://randomuser.me/api/?results=5000") .then(t=>t.JSON()) .then(result=>console.log(result))

Error Message :

[Running] node "c:\Users\huseyinoz\Desktop\javatest\tempCodeRunnerFile.js" c:\Users\huseyinoz\Desktop\javatest\tempCodeRunnerFile.js:2 fetch("https://randomuser.me/api/?results=5000") ^

ReferenceError: fetch is not defined at Object. (c:\Users\huseyinoz\Desktop\javatest\tempCodeRunnerFile.js:2:1) at Module._pile (module.js:652:30) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32)

Share Improve this question edited Nov 15, 2018 at 14:09 hsyn.ozkara asked Nov 15, 2018 at 14:06 hsyn.ozkarahsyn.ozkara 1272 silver badges11 bronze badges 9
  • 2 you have a typo - fecth instead of fetch – barbsan Commented Nov 15, 2018 at 14:07
  • Result is same error :( ReferenceError: fetch is not defined – hsyn.ozkara Commented Nov 15, 2018 at 14:08
  • 1 also arrow function is not in vanilla JS. You need to change () => {} to normal function declaration – blaz Commented Nov 15, 2018 at 14:09
  • well guessing the engine does not support it.... import fetch so it can. – epascarello Commented Nov 15, 2018 at 14:09
  • 1 @blaz — Arrow functions are defined in the ECMAScript spect. They are entirely vanilla. – Quentin Commented Nov 15, 2018 at 14:11
 |  Show 4 more ments

2 Answers 2

Reset to default 3

fetch (note spelling) has to be provided by something.

It does not exist in the ECMAScript specification, but nor does any form of input or output function. Such functions must be provided by the host environment.

Browsers provide the Fetch API.

Node.js doesn't, but implementations of it are available in packages in NPM.

So pick an implementation (such as this one).

Install it (npm install fetch).

Then import it into your script:

const fetch = require("fetch").fetchUrl

Updating answers / ments to status as of 2021:

fetch() is now defined in ES6:

Javascript ES6 (also known as ECMAScript 2015, ES6 or ECMAScript 6), handles the fetch()-API and it is supported by all major browsers.

Documentation at MDN Using Fetch

发布评论

评论列表(0)

  1. 暂无评论