2014-09-24 177 views
0

我正在使用第三方API來獲取值。例如,在這裏得到一個國家的價值就是樣本。JavaScript API不能在函數內工作

var _ttprofiles = _ttprofiles || []; 
    _ttprofiles.profiles = []; 
    _ttprofiles.push(['_sync', false]); 
    var callback = function(profiles) { 

     console.log(profiles.getCountry); 
    }; 

    _ttprofiles.push(['_registerCallback', callback]); 
    _ttprofiles.push(['_enableServices']); 

現在我很簡單地在這樣的JavaScript文件中使用。

var _ttprofiles = _ttprofiles || []; 
    _ttprofiles.profiles = []; 
    _ttprofiles.push(['_sync', false]); 
    var callback = function(profiles) { 

     console.log(profiles.getCountry); 
    }; 
    _ttprofiles.push(['_registerCallback', callback]); 
    _ttprofiles.push(['_enableServices']); 

    (function() { 
     try { 
     // getValues(); 
     } catch (err) { 



     } 
    })(); 

我在該國獲得價值。這裏1是我的螢火蟲

enter image description here

的屏幕快照但是,當我把這個方法中的,然後我得到空字符串

(function() { 
     try { 
      getValues(); 
     } catch (err) { 


     } 
    })(); 

    function getValues() 
    { 
       var _ttprofiles = _ttprofiles || []; 
    _ttprofiles.profiles = []; 
    _ttprofiles.push(['_sync', false]); 
    var callback = function(profiles) { 

     console.log(profiles.getCountry); 
    }; 
    _ttprofiles.push(['_registerCallback', callback]); 
    _ttprofiles.push(['_enableServices']); 


     } 

然後我得到空字符串,爲什麼?這裏是螢火蟲

enter image description here

的屏幕截圖,可以有人建議我的JavaScript調試好。我在Firefox中使用螢火蟲,但我的控制檯不工作我也嘗試鉻調試器,但它有同樣的問題,雖然控制檯的螢火蟲在鉻工作

+0

似乎沒有要在這個問題上的信息足以讓一個有意義的答案。什麼是第三方API? 'profiles'從哪裏來? – TML 2014-09-24 04:58:40

+0

我在我的索引頁中使用所以配置文件來自他們的 – Coder 2014-09-24 05:00:31

+0

我不明白'var _ttprofiles = _ttprofiles || [];',從哪裏得到'_ttprofiles'? – 2014-09-24 05:01:14

回答

0

看起來像Nicolas一樣,但是你必須創建一個位於全局命名空間中的_ttprofiles變量,而不是像第三方庫已經創建該變量那樣。所以,我試着把它傳遞給你的函數調用,它似乎工作。

我不得不承認我不完全確定輸出應該是什麼。我在警報框中找到了'我們'。

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<title></title> 
 
<script src="http://d.tailtarget.com/profiles.js"></script> 
 
</head> 
 
<body> 
 
<script> 
 
(function() { 
 
     try { 
 
      getValues(_ttprofiles); 
 
     } catch (err) { 
 
      console.log(err); 
 
     } 
 
    })(); 
 

 
function getValues(){ 
 
    _ttprofiles = _ttprofiles || []; 
 
    _ttprofiles.profiles = []; 
 
    _ttprofiles.push(['_sync', false]); 
 
    var callback = function(profiles) { 
 
     alert(profiles.getCountry); 
 
    }; 
 
    _ttprofiles.push(['_registerCallback', callback]); 
 
    _ttprofiles.push(['_enableServices']); 
 
} 
 
</script> 
 
</body> </html>

0

你的第三方也許看_ttprofiles更改,因爲它只是一個數組並沒有功能被調用來處理其內容。

試着讓_ttprofiles全球在您的getValues()功能:刪除第一個var

+0

你是什麼意思全球 – Coder 2014-09-24 05:14:01

+0

我試過這個,但仍然沒有工作 – Coder 2014-09-24 05:19:33