Skip to content

Instantly share code, notes, and snippets.

@xgqfrms-GitHub
Created January 14, 2018 13:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xgqfrms-GitHub/64972e969d7491a7f845b9d69a9ba743 to your computer and use it in GitHub Desktop.
Save xgqfrms-GitHub/64972e969d7491a7f845b9d69a9ba743 to your computer and use it in GitHub Desktop.
whatwg-fetch & fetch polyfill

whatwg-fetch & fetch polyfill

https://davidwalsh.name/fetch

    

import 'whatwg-fetch';
// fetch polyfill

const url = <code>https://abc.xgqfrms.xyz/some-url</code>;

const headers = new Headers({
    'Content-Type': 'text/plain',
    'X-My-Custom-Header': 'CustomValue'
});

const datas = JSON.stringify(
    {
        user: "username",
        pwd: "password"
    }
);

const method = <code>POST</code>;

const request = new Request(url, {
    headers: headers,
    method: 'POST', 
    mode: 'cors', 
    redirect: 'follow',
    body: datas
});

fetch(
    request
).then(
    (response) => response.json()
).catch(
    (err) => {
        console.log(<code>err</code>, err);
        throw new Error("Whoops, An Error occurred!");
    }
);
@xgqfrms-GitHub
Copy link
Author

xgqfrms-GitHub commented Jan 14, 2018

https://gist.github.com/xgqfrms-GitHub/039e67b096ce204379b2aba95fbd494d

https://gist.github.com/xgqfrms-GitHub/b252342f28497eadc96134a5873814fb


https://learning.xgqfrms.xyz/webpack-next/000-xyz/no-jquery-modal/modal/libs/test.html

https://cdn.xgqfrms.xyz/json/news/560956639429
https://cdn.xgqfrms.xyz/json/news/560956639429.json

https://github.com/gildata/RAIO/issues/280#issuecomment-355833553

fetch(`https://cdn.xgqfrms.xyz/json/badges.json`)
.then((res) => res.json())
.then((data) => console.log(data));


fetch(`https://cdn.xgqfrms.xyz/json/badges.json`)
.then((res) => res.json())
.then((data) => {
    console.log(data.user);
    console.log(data.courses);
    console.log(data.badges);
});

@xgqfrms-GitHub
Copy link
Author

https://learning.xgqfrms.xyz/webpack-next/000-xyz/no-jquery-modal/modal/libs/test.html

https://cdn.xgqfrms.xyz/json/news/560956639429
https://cdn.xgqfrms.xyz/json/news/560956639429.json

https://github.com/gildata/RAIO/issues/280#issuecomment-355833553

fetch(`https://cdn.xgqfrms.xyz/json/badges.json`)
.then((res) => res.json())
.then((data) => console.log(data));


fetch(`https://cdn.xgqfrms.xyz/json/badges.json`)
.then((res) => res.json())
.then((data) => {
    console.log(data.user);
    console.log(data.courses);
    console.log(data.badges);
});

@xgqfrms-GitHub
Copy link
Author

fetch(`https://cdn.xgqfrms.xyz/json/news/560956639429`)
.then((res) => res.json())
.then((data) => console.log(data));

image

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment