【问题标题】:How to handle responseText for Node.js?如何处理 Node.js 的 responseText?
【发布时间】:2017-09-22 00:23:57
【问题描述】:

我有一个 javascript 函数,如下所示,我将它托管在 S3 上

function myFunction() {

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
    if(this.readyState == 4 && this.status == 200){
        document.getElementById("testing").innerHTML = this.responseText;
    }
};
xhttp.open("GET","https://id.execute-api.ap-southeast-1.amazonaws.com/prod/lambdafunction", true);
xhttp.send();

}

这个lambdafunction写在Node.js如下

'use strict';

console.log('Loading function');

exports.handler = (event, context, callback) => {

let response = {
    statusCode: '200',
    body: JSON.stringify({ error: 'you messed up!' }),
    headers: {
        'Content-Type': 'application/json',
    }
};
context.succeed(response);
//callback(null, context);  // Echo back the first key value
//callback('Something went wrong');
};

我期望的是带有 id 测试的 div 将被 error: 'you messed up! 取代,但什么也没发生?我可以知道哪个部分出了问题吗?

【问题讨论】:

    标签: node.js amazon-web-services aws-lambda aws-api-gateway


    【解决方案1】:

    看起来您正在使用(非常)旧节点 v.0.10.42 的 api。

    您似乎更有可能使用较新的版本,因此您应该:

    callback(null, JSON.stringify({ error: 'you messed up!' })); 
    // if you are not using proxy integration
    

    callback(null, response)
    // if you set up the function with proxy integration
    

    如果这没有帮助,那么了解当您直接访问该 url 时得到什么以及您是否在 AWS 日志中看到任何内容时会很有用。您还应该能够从 AWS 控制台直接调用 lambda 函数,这使得测试更加容易。

    【讨论】:

      猜你喜欢
      • 2017-10-21
      • 1970-01-01
      • 2012-12-12
      • 2020-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-04
      • 2015-10-16
      相关资源
      最近更新 更多