我在這裏住了,很想知道我mising。爲什麼我看不到我的對象的屬性
我只是重新寫一些代碼和清理一些東西了。我有一個方法,執行ajax調用並返回一個json字符串。因爲我在這裏使用了這個方法,所以我抽象出了這個方法在這個名字空間中。
這是JSON對象返回
{"charge_ID":"4","price":"37","description":"corporate rate"}
這是抽象方法。
wwdb.getChargeRate = function(chargeID){
var rate = {}, prop;
$.post(baseUrl + 'dashboard_job/jx_get_charge_rate',
{
'charge_ID': chargeID
},
function(data){
for(prop in data) {
rate[prop] = data[prop];
}
},
'json'
);
return rate;
};
我以爲我只是能夠分配數據的速度,但不得不迭代通過,以得到它的工作。事實上,我認爲我可以從$ .post()方法的function()部分直接返回數據,但那不起作用。所以問題1是爲什麼我不能只返回數據?
但更基本的和令人沮喪的是,在下面的代碼我無法訪問對象屬性的事實。問題2是爲什麼?
$('#charge_rate_slt').change(function(){
var t = $(this), v = $(t).val(), rate;
console.log(wwdb.getChargeRate(v)); //returns object ok
rate = wwdb.getChargeRate(v); //assigns object ok
console.log(rate); //displays in firebug as object with properties
console.log(rate.price) //undefined
for(p in rate)
console.log(p); //undefined?
});
你有沒有聽說過的AJAX的異步特性?的 – Musa 2012-07-29 06:22:14
可能重複[無法返回xmlhttp.responseText?](http://stackoverflow.com/questions/10369839/cant-return-xmlhttp-responsetext) – DCoder 2012-07-29 06:51:12