2015-10-09 49 views
0

我試圖建立某種形式的AJAX連接檢查的對象,但由於某種原因,我只是不能得到它的工作:/阿賈克斯連接檢查對象沒有返回值

對象:

function conn(data){ 

this.conn = false, 

this.check = function(data){ 
    if(data){ 
     console.log("data: "+data)//Returns true 
     this.conn = data; 
     } else { 
     this.callServer(); 
     } 
}, 
this.callServer = function(){ 
    $.get(ApiUrl("online")) 
    .done(function(data){}) 
    .fail(function(data){}) 
    .always(function(data){var c = new conn();c.check(data)}); 
    } 
} 

用法:

var code = $(".login-form input#pincode").val(); 
var con = new conn(); 
con.check(); 
    console.log("connection: "+ con.conn); // returns false 
    code = code.match(/[0-9]/g); 
    if (code === null || code.length !== 6) { 
     Alert("De code moet bestaan uit zes cijfers"); 
    } else { 
     if(con.conn === true){ 
       //Do stuff 
     }  

函數 'ApiURL' 是創建了該URL所以不介意功能

+0

你可以調用con.check();沒有數據參數 – Max

+0

它有一個if語句。 –

回答

0

使用對象構造函數創建新對象時,缺少「數據」。 var con = new conn();需要是var con = new conn(<data need to be inserted here>);這就是爲什麼它不工作,我猜。

+0

數據參數是可選的。當調用'check'方法時,它會檢查數據...如果沒有,那麼ajax調用會對返回的數據執行回調'check'方法 –