json - SyntaxError:JSON.parse 中位置 1 的 JSON 中的意外标记 <)
问题描述
我正面临这个错误,我不知道我的代码的问题出在哪里,错误说 undefined:2 SyntaxError: Unexpected token < in JSON at position 1 at JSON.parse ()
我的代码,我不知道问题出在哪里,谁能帮助我
function sendTextMessage(recipientId, messageText) {
let text = messageText.toLowerCase();
// Get Data From a Google Sheet
var google_sheet_json = "https://spreadsheets.google.com/feeds/list/" +
process.env.GOOGLE_SHEET_ID +"/od6/public/values?alt=json";
request.get(google_sheet_json, function (err, res, body) {
if (!err) {
var bot_script_obj = JSON.parse(body);
var all_bot_scripts = bot_script_obj.feed.entry;
var i = 0;
var len = all_bot_scripts.length;
var all_keywords = [];
for (; i < len; ) {
if (text.includes(bot_script_obj.feed.entry[i].gsx$incoming.$t)){
var messageText = bot_script_obj.feed.entry[i].gsx$outgoing.$t;
var messageData = {
recipient: {
id: recipientId
},
message: {
text: messageText
}
};
callSendAPI(messageData);
}
i++;
}
} else {
console.log(err);
}
});
}
function callSendAPI(messageData) {
request({
uri: 'https://graph.facebook.com/v2.6/me/messages',
qs: { access_token: process.env.PAGE_ACCESS_TOKEN },
method: 'POST',
json: messageData
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
var recipientId = body.recipient_id;
var messageId = body.message_id;
console.log("Successfully sent generic message with id %s to recipient %s",
messageId, recipientId);
} else {
console.error("Unable to send message.");
console.error(response);
console.error(error);
}
});
}
解决方案
似乎body
收到的参数不是 JSON,而是 HTML。
推荐阅读
- java - 如何用字符串替换器替换特殊字符
- python - 使用 setup.py install 构建 docker 镜像比先安装 pip 慢得多
- elixir - 如何在 Elixir 地图中将代码点作为字符表示返回?
- android - 儿童拦截触摸事件的 MotionLayout 问题
- angularjs - 在 Angularjs 应用页面上快速单击时无法加载
- javascript - JavaScript ES6 Promise 是否有一个标准的 then() 程序将多个耗时的 Promise 与异步任务链接起来?
- c# - Tag Helper - 添加集合中的所有属性
- c++ - 如何使虚函数接受仅在派生类中定义的数据类型?
- macos - 虚拟框符号 MacOS
- python - 熊猫每天迭代数据框