JavaScript Fetch API'si

Fetch API arayüzü, Web tarayıcılarının Web sunucusuna HTTP istekleri göndermesine izin verir.

Artık XMLHttpRequest gerekmez.

Tarayıcı Desteği

Tablodeki rakamlar, Fetch API'yi tam olarak destekleyen ilk tarayıcı sürümünü belirtmektedir:

Chrome IE Firefox Safari Opera
Chrome 42 Edge 14 Firefox 40 Safari 10.1 Opera 29
June 2011 August 2016 August 2015 March 2017 April 2015

Fetch API Example

The following example retrieves a file and displays the content:

Example

fetch(file)
.then(x => x.text())
.then(y => myDisplay(y));

Try It Yourself

Since Fetch is based on async and await, the above example can be written like this for better understanding:

Example

async function getText(file) {
  let x = await fetch(file);
  let y = await x.text();
  myDisplay(y);
}

Try It Yourself

Even better: use easy-to-understand names instead of x and y:

Example

async function getText(file) {
  let myObject = await fetch(file);
  let myText = await myObject.text();
  myDisplay(myText);
}

Try It Yourself