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);
}
}
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);
}
}
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();