Skip to content
Branch: master
Find file Copy path
Find file Copy path
2 contributors

Users who have contributed to this file

@tjcrowder @chrisdavidmills
57 lines (47 sloc) 1.32 KB
<!DOCTYPE html>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width">
<title>Fetch with init then Request example</title>
<link rel="stylesheet" href="">
<!--[if lt IE 9]>
<script src="//"></script>
<h1>Fetch with init then Request example</h1>
<img src="">
var myImage = document.querySelector('img');
var myHeaders = new Headers();
myHeaders.append('Content-Type', 'image/jpeg');
var myInit = {
method: 'GET',
headers: myHeaders,
mode: 'cors',
cache: 'default'
var myRequest = new Request('flowers.jpg');
fetch(myRequest, myInit)
.then(function(response) {
if (!response.ok) {
throw new Error("HTTP error, status = " + response.status);
return response.blob();
.then(function(blob) {
var objectURL = URL.createObjectURL(blob);
myImage.src = objectURL;
.catch(function(error) {
var p = document.createElement('p');
document.createTextNode('Error: ' + error.message)
document.body.insertBefore(p, myImage);
You can’t perform that action at this time.