从javascript对象返回值
问题描述:
我创建了一个javascript对象,并尝试从中获取som数据. 我使用jquery ajax来获取发送一些值,然后从php脚本重现它们. 这行得通,但是当我尝试使用我的javascript对象的实例显示该值时,我会得到未定义".
I have created a javascript object and im trying to fetch som data from it. Im using jquery ajax to fetch send some values and then returing them from a php script. this works, but when i try to use an instanse of my javascript object to dispaly the value i get 'undefined'.
对象:
var getDBresults = (function () {
function getResult(url,TableName ,callback){
$.ajax({
url: url,
type: 'POST',
data: {
'table':TableName,
},
success: function(data){
callback(data);
},
error: function(event){
alert(event.error);
}
});
}
return {
getAllVideoes: function(){
getResult("getAllResults.php", "videoer", function(data){
console.log(data); //Works
return data;
});
},
}
})();
在我的其他脚本中:
var obj = getDBresults;
var data = obj.getAllVideoes(); //this runs, and produce the log showed above
console.log(data) //results in undefined
console.log(obj.getAllVideoes()) //results in undefined