Skip to content

Instantly share code, notes, and snippets.

@awn-git
Last active January 9, 2017 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awn-git/2eac98c3e9f89cb5fa1e6ed2a70e3598 to your computer and use it in GitHub Desktop.
Save awn-git/2eac98c3e9f89cb5fa1e6ed2a70e3598 to your computer and use it in GitHub Desktop.

test

コード

jQuery

class jQueryAgent {
    constructor() {}

    set(url) {
        this.url = url;
    }

    get() {
        $.ajax({
            type: 'GET',
            url: this.url,
            success: this.f_jQuery,
            error: this.f_jQuery
        });
    }

    f_jQuery(data, textStatus, errorThrown) {
        console.log(`status: ${errorThrown.status}`);
        //console.log(`status: ${errorThrown.statusText}`);
        console.log(`statusText: ${textStatus}`);
        console.log(data);
        console.dir(errorThrown);
    }
}

XMLHttpRequest

class XHRAgent {
    constructor() {
        let xhr = new XMLHttpRequest();
        xhr.addEventListener("loadend", this.f_xhr);
        this.xhr = xhr;
    }

    set(url) {
        let xhr = this.xhr;
        xhr.open("GET", url);
        this.xhr = xhr;
    }

    get() {
        let xhr = this.xhr;
        xhr.send();
    }

    f_xhr(ev) {
        let resp = ev.target;
        console.log(`status: ${resp.status}`);
        console.log(`statusText: ${resp.statusText}`);
        console.log(resp.response);
        console.dir(resp);
    }
}

Fetch

class FetchAgent {
    constructor() {
        let headers = new Headers();
        let init = {
            method: "GET",
            headers: headers
        };
        this.init = init;
    }

    set(url) {
        let request = new Request(url, this.init);
        this.request = request;
    }

    get() {
        fetch(this.request, this.init).then(this.f_fetch);
    }

    f_fetch(response) {
        console.log(`status: ${response.status}`);
        console.log(`statusText: ${response.statusText}`);
        response.text().then((text) => console.log(text));
        console.dir(response);
    }

}

テスト

/* test */
var ja = new jQueryAgent();

//失敗するケース
ja.set("http://uni.open2ch.net/ajax/get_ank.cgi/test/read.cgi/gameswf/1483937104/585");
ja.get();

//成功するケース
ja.set("https://randomuser.me/api/");
ja.get();
/* test */
var xa = new XHRAgent();

//失敗するケース
xa.set("http://uni.open2ch.net/ajax/get_ank.cgi/test/read.cgi/gameswf/1483937104/585");
xa.get();

//成功するケース
xa.set("https://randomuser.me/api/");
xa.get();
/* test */
var fa = new FetchAgent();
//失敗するケース
fa.set("http://uni.open2ch.net/ajax/get_ank.cgi/test/read.cgi/gameswf/1483937104/585");
fa.get();

//成功するケース
fa.set("https://randomuser.me/api/");
fa.get();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment