こちらのhtml5rocksWebサイトでXMLHttpRequest2のガイドをフォローしています。JavaScriptでクラスを作成する方法も学んでいます。すべてが正しいように見えますが、jsfiddleでこのコードをテストすると、ifステートメントから「エラー」が2回返され、応答が未定義を返します。これはクラスの問題だと思いますか?
function Ajax(parameters) {
this.type = parameters.type;
this.url = parameters.url;
this.format = parameters.format;
this.send = parameters.send;
this.xhr = new XMLHttpRequest();
}
Ajax.prototype.initialize = function () {
this.xhr.open(this.type, this.url, true);
this.xhr.responseType = this.format;
this.xhr.onload = this.process();
this.xhr.send(this.send);
};
Ajax.prototype.process = function () {
var self = this;
if (self.xhr.readyState === 4 && self.xhr.status === 200) {
console.log(JSON.parse(self.xhr.response));
} else {
console.log("error");
}
};
var test = new Ajax({type:"GET", url:"http://ip.jsontest.com/", format:"text", send:""});
test.initialize();
console.log(test.process());