Skip to content

Instantly share code, notes, and snippets.

@xgqfrms-GitHub
Last active July 5, 2017 06:33
Show Gist options
  • Save xgqfrms-GitHub/68af127cb3ec33587ee5bffe9810fb1b to your computer and use it in GitHub Desktop.
Save xgqfrms-GitHub/68af127cb3ec33587ee5bffe9810fb1b to your computer and use it in GitHub Desktop.
AJAX & XMLHttpRequest & Fetch & Worker & jquery ajax
@xgqfrms-GitHub
Copy link
Author

@xgqfrms-GitHub
Copy link
Author

xgqfrms-GitHub commented May 16, 2017

fetch

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

<script>
    fetch('https://github-cloud.s3.amazonaws.com/', {
        method: 'post',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            "title":   "Add a blogpost about Angular2",
            "dueDate": "2015-05-23T18:25:43.511Z",
            "done": false
        })
    }).then(function(response) {
            return response.json()
    }).then(function(json) {
            console.log('parsed json: ', json)
    }).catch(function(error) {
          console.log('parsing failed: ', error)
    });




    fetch('https://cdn.xgqfrms.xyz/json/cats.json', {
        method: 'get',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        }
    }).then(function(response) {
            return response.json()
    }).then(function(json) {
            console.log('parsed json: ', json)
    }).catch(function(error) {
          console.log('parsing failed: ', error)
    });
</script>

@xgqfrms-GitHub
Copy link
Author

React JSON-Server

json-server

$ npm i -g json-server

$ npm i -D json-server

faker.js

https://www.npmjs.com/package/faker

$ npm i -D faker

yarn

$ yarn add -D json-server

$ yarn add -D faker

@xgqfrms-GitHub
Copy link
Author

let myImage = document.querySelector('img');

let myHeaders = new Headers();
myHeaders.append('Content-Type', 'image/png');

const myInit = {
    method: 'GET',
    headers: myHeaders,
    mode: 'cors',
    cache: 'default'
};

let myRequest = new Request('https://cdn.xgqfrms.xyz/logo/icon.png');

fetch(myRequest, myInit)
.then(function(response) {
    return response.blob();
})
.then(function(blob) {
    var objectURL = URL.createObjectURL(blob);
    myImage.src = objectURL;
});

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