简体   繁体   English

使用 Lambda 函数在 Amazon S3 中发出 Ajax 请求

[英]Making an Ajax request in Amazon S3 using a Lambda function

I'm trying to fetch a JSON response with weatherdata coming from the Netatmo Cloud, using a lambda function/javascript in Amazon S3 AWS.我正在尝试使用 Amazon S3 AWS 中的 lambda 函数/javascript 来获取来自 Netatmo 云的天气数据的 JSON 响应。 I am first trying to fetch a token using the following method.我首先尝试使用以下方法获取令牌。 It seems that the dollar sign is not recognized.似乎美元符号不被识别。 What gives?是什么赋予了?

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...");  

}

Looks like your trying to use a jQuery ajax method.看起来您正在尝试使用 jQuery ajax 方法。 If jQuery isn't loaded this won't work.如果未加载 jQuery,这将不起作用。 I'm not very familiar with the AWS lambda interface so if it is possible to load jQuery before the script is run, that would seem to be your best bet.我对 AWS lambda 接口不是很熟悉,所以如果可以在脚本运行之前加载 jQuery,那似乎是你最好的选择。

Your other option would be a vanilla javascript XMLHttpRequest.您的另一个选择是普通的 javascript XMLHttpRequest。 I peeked around at Netatmo's documentation and it looks like this should work我看了看 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM