我想從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
與$.get("http://localhost:9000/x", function(response) { alert(response) });
這樣的內容調用它,它就可以工作 - 但我想保存回覆。我認爲這是$.get
-方法的範圍問題。
這是怎麼回事?
爲什麼工作與'alert'的代碼? (''.get(「http:// localhost:9000/x」,function(response){alert(response)});') – Themerius
因爲您可能在您的計算機上本地測試網站。所以它是相同的來源,你可以處理你自己的數據。 – chucktator
有些事情是可能的。像警報一樣,填寫顯示的div,console.log。有些不是。 –