在 lambda 函数中发送 JSON 响应时无服务器离线响应问题
Serverless offline response issue while sending JSON response in lambda function
var result = [{
count : 10,
data : [{"id":11,"id":22}]
}];
var response = {
statusCode: 200,
count: result.length,
body: result
};
callback(null, response);
控制台错误
According to the API Gateway specs, the body content must be
stringified. Check your Lambda response and make sure you are invoking
JSON.stringify(YOUR_CONTENT) on your body object
这里的错误给你解决方法。
API 网关的回调需要一个字符串而不是 javascript 对象。在将其传递给回调之前,您必须对其进行字符串化:
var result = [{
count : 10,
data : [{"id":11,"id":22}]
}];
var response = {
statusCode: 200,
count: result.length,
body: result
};
callback(null, JSON.stringify(response));
编辑:
然后在客户端解析 JSON 字符串以将其返回到一个对象(此示例假设您的客户端也是 Javascript):
var myObject = JSON.parse(responseBody);
var result = [{
count : 10,
data : [{"id":11,"id":22}]
}];
var response = {
statusCode: 200,
count: result.length,
body: result
};
callback(null, response);
控制台错误
According to the API Gateway specs, the body content must be stringified. Check your Lambda response and make sure you are invoking JSON.stringify(YOUR_CONTENT) on your body object
这里的错误给你解决方法。
API 网关的回调需要一个字符串而不是 javascript 对象。在将其传递给回调之前,您必须对其进行字符串化:
var result = [{
count : 10,
data : [{"id":11,"id":22}]
}];
var response = {
statusCode: 200,
count: result.length,
body: result
};
callback(null, JSON.stringify(response));
编辑:
然后在客户端解析 JSON 字符串以将其返回到一个对象(此示例假设您的客户端也是 Javascript):
var myObject = JSON.parse(responseBody);