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

标签 javascript ajax amazon-s3 aws-lambda

我正在尝试使用 Amazon S3 AWS 中的 lambda 函数/javascript 来获取包含来自 Netatmo 云的天气数据的 JSON 响应。 我首先尝试使用以下方法获取 token 。美元符号似乎无法识别。给出了什么?

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

}

最佳答案

看起来您正在尝试使用 jQuery ajax 方法。如果 jQuery 没有加载,这将不起作用。我对 AWS lambda 接口(interface)不是很熟悉,因此如果可以在脚本运行之前加载 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);
}

关于javascript - 使用 Lambda 函数在 Amazon S3 中发出 Ajax 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33466043/

相关文章:

jquery - 在java中解码EncodeURIComponent

javascript - 如何从 javascript 打印 WordPress 简码

javascript - 在客户端用 JavaScript 逐行读取文件

javascript - 在 jQuery 中追加

javascript - 获取 iFrame 中 Ajax 函数的结果

javascript - 图像加载后进行 AJAX 调用,一次一张

amazon-s3 - Athena - 制作当前/最新分区

amazon-s3 - 通过 aws 命令将内容编码设置为特定文件

hadoop - brew 安装了 apache-spark 无法访问 s3 文件

javascript - 如何将 Waze 数据与我的网页整合?