node.js - 尝试解析 http 请求的 json 时,位置 1 处的 JSON 中出现意外令牌 o 错误
问题描述
我是 node.js 的新手,希望能得到一些帮助。
我目前正在编写代码,它只是通过解析 json 来获取请求 id,然后将该 id 中继到另一个 http 调用,以派生令牌,然后通过 puppeteer 输入到 html。
*目前,当我尝试通过 nodejs 的 json parse 命令解析 json 时,我收到错误:
UnhandledPromiseRejectionWarning:SyntaxError:位置 1 处 JSON 中的意外标记 o
我的意图是导出属性“请求”的值,因此可以在后续步骤中将其作为常量引用。
但是,我收到上述错误。
当我发出 http 请求时,JSON 格式如下所示。
{“状态”:1,“请求”:“123452590”}
(此测试用例中的请求值为 123452590,这是我想要获取并存储为常量的值
任何帮助将非常感激。
目前,代码的结构如下:
method: 'userrecaptcha',
key: xxx
googlekey: 'xxx',
pageurl: 'http://test.ca',
json: 1
};
var request = require('request');
const response = await request.post('http://2captcha.com/in.php', {form: formData});
const requestId = JSON.parse(response).request;
console.log (requestId)
async function pollForRequestResults(key, id, retries = 30, interval = 1500, delay = 15000) {
await timeout(delay);
return poll({
taskFn: requestCaptchaResults(key, id),
interval,
retries
});
}
function requestCaptchaResults(key, requestId) {
const url = `http://2captcha.com/res.php?key=${key}&action=get&id=${requestId}&json=1`;
return async function() {
return new Promise(async function(resolve, reject){
const rawResponse = await request.get(url);
const resp = JSON.parse(rawResponse);
if (resp.status === 0) return reject(resp.request);
resolve(resp.request);
});
}
}
const timeout = millis => new Promise(resolve => setTimeout(resolve, millis))
})()```
const requestId = JSON.parse(response).request;
解决方案
推荐阅读
- javascript - 如果满足条件,有什么方法可以为特定列表项添加边框?
- ios - Xamarin 表单视频缩略图:尝试在 IOS 13.1.3 设备上创建视频缩略图表单本地存储时出错
- unit-testing - 我能写什么,这样我就可以测试从我的账户中取钱
- javascript - 如何减少一小段代码的重复性?
- javascript - 从数据到 asyncData
- javascript - 我的偏移量似乎正在工作,但分页在每个页面上都显示相同的列表
- vba - Outlook Application_Quit 事件未触发
- python - 烧瓶 - 如何发送数据并将其取回
- jquery - jQuery Object 在第二轮循环中重写自己
- c# - 将类的 IEnumerable 属性绑定到 kendo mvc 网格列