URL からコンテンツを読み込んで、コードで使用したいと考えています。私はクロージャーとこれを試しました:
function getStringFromURL(url) {
var getter = function() {
this.result = "undef";
this.func = function(response) {
this.result = response;
};
};
var x = new getter();
$.get(url, x.func);
return x.result; // the it returns "undef" and not the wanted response
}
何も機能しませんでした。コンテンツを取得することはできませんが、alert
like$.get("http://localhost:9000/x", function(response) {
alert(response)
});
で呼び出すと動作しますが、応答を保存したいと思います。-メソッドの範囲に問題があると思います$.get
。
これの何が問題なのですか?