2015-11-01 26 views
0

我試圖從Amazon S3 AWS中使用lambda函數/ javascript獲取來自Netatmo雲的weatherdata的JSON響應。 我第一次嘗試使用以下方法獲取令牌。看來美元符號不被認可。是什麼賦予了?使用Lambda函數在Amazon S3中發出Ajax請求

function getNetatmoData(){ 
var clientId = "******"; 
var clientSecret = "******"; 

var userId="******@******.com"; 
var parola="******"; 
var formUserPass = { client_id: clientId, 
client_secret: clientSecret, 
username: userId, 
password: parola, 
scope: 'read_station', 
grant_type: 'password' }; 

$.ajax({ 
async: false, 
url: "https://api.netatmo.net/oauth2/token", 
    type: "POST", 
    dataType: "json", 
    data: formUserPass, 
    success: function(token){ 
     // do something awesome with the token.. 
    } 

}); 


console.log("http request successful..."); 

} 

回答

1

看起來像你試圖使用jQuery ajax方法。如果沒有加載jQuery,這將不起作用。我對AWS lambda界面不是很熟悉,所以如果可以在腳本運行之前加載jQuery,這似乎是你最好的選擇。

您的其他選項將是香草JavaScript的XMLHttpRequest。我偷看在Netatmo的文件,它看起來像這個應該工作

function getNetatmoData(){ 
var clientId = "******"; 
var clientSecret = "******"; 

var userId="******@******.com"; 
var parola="******"; 
var formUserPass = { client_id: clientId, 
    client_secret: clientSecret, 
    username: userId, 
    password: parola, 
    scope: 'read_station', 
    grant_type: 'password' }; 
var req = new XMLHttpRequest(); 
req.open('POST',"https://api.netatmo.net/oauth2/token", false); 
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); 

req.onload = function (e) { 
    if (req.status == 200) { 

     console.log('http request was successful', req.response) 
    } 
    else if (req.status == 400) { 
     console.log('There was an error') 
    } 
    else { 
     console.log('There was something else that went wrong') 
    } 
} 
req.onerror = function (e) { 
    // Do something about the error 
    console.log("There was an error", req.response); 
} 
req.send(formUserPass); 
} 
+0

感謝您的意見!將嘗試並找回你! –

相關問題