2012-06-11 138 views
0
var getPageInhalt : function (id){ 
    var restmethod = "http://localhost:1212/getPageById/"+id+"/jsonp.aspx?callback=?"; 

    $.ajax({ 
     url: restmethod, 
     type: "GET", 
     contentType: "application/json", 
     async: false,       
     dataType: 'jsonp', 
     cache: false, 
     error: function(){ 
      return false; 
     }, 
     success: function(data){ 
      console.log(data); --> balblal 
      return data; 
     } 
    }); 
} 

console.log(getPageInhalt(2));-->undefined ????? 
+1

如果我有1£有關從異步函數返回值的每一個問題... –

+1

'getPageInhalt'函數中沒有返回語句。也就是說,請不要發出同步的Ajax請求。 –

回答

2

後,雖然我與同步AJAX(在技術上它是一個矛盾..),你將不得不從getPageInhalt功能,而不是從AJAX回調返回的值不同..

var getPageInhalt = function (id){ 
    var restmethod = "http://localhost:1212/getPageById/"+id+"/jsonp.aspx?callback=?", 
     resultValue; 

    $.ajax({ 
     url: restmethod, 
     type: "GET", 
     contentType: "application/json", 
     async: false,       
     dataType: 'jsonp', 
     cache: false, 
     error: function(){ 
      resultValue = false; 
     }, 
     success: function(data){ 
      console.log(data); //--> balblal 
      resultValue = data; 
     } 
    }); 

    return resultValue; 
} 

這裏是一個演示http://jsfiddle.net/gaby/qHY7g/

+0

is not working!console.log(getPageInhalt(2)); - > undefined – samibel

+0

將'var getPageInhalt:'改爲'var getPageInhalt ='因爲這是一個語法錯誤..(*使用'='而不是': '*) –

+0

var page = { getPageInhalt:function(id){--my function ---}} – samibel