【发布时间】:2020-11-14 09:49:01
【问题描述】:
这是我的第一篇 Stack Overflow 帖子。我在尝试使用 API 将记录添加到 Kintone 应用程序时遇到了困难。我正在使用 ajax 发送到使用 nodejs 的 AWS lambda 函数。我知道我的 AWS api 网关正在工作。我只是想不通为什么 JSON 对象没有发布到添加记录端点。
这是我的 JSON 对象:
body: {
app: 1,
record: {
Text: {
value: 'Sample'
},
Number: {
value: 1
}
}
}
这是我的 ajax 帖子:
$.ajax({
type: "POST",
url: 'http://my-aws-endpoint/add-record',
dataType: 'json',
data: JSON.stringify({ body })
// data: body
}).done(function(response) {
console.log(response);
});
这是我的 nodejs:
var querystring = require('querystring');
var https = require('https');
var DOMAIN = 'mydomain.com';
var AUTH_VALUE = 'myauthvalue;
var headers = {
'api-token': 'myapitoken',
'Content-Type': 'application/json'
};
exports.handler = function (event, context) {
var postData = querystring.stringify(
event.body
);
var options = {
hostname: DOMAIN,
port: 443,
path: '/k/v1/add-record.json',
method: 'POST',
headers: headers
};
var req = https.request(options, function (res) {
var data = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
context.done(null, JSON.parse(data));
})
});
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
req.write(postData);
req.end();};
这是我得到的回应:
{code: "CB_IJ01", id: "FyttteBzB4eDaWEwoNUO", message: "Invalid JSON string."}
提前感谢您的帮助!
【问题讨论】:
标签: node.js json ajax aws-lambda