首页 > 解决方案 > 尝试解析 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;




标签: node.jsjsonpuppeteer

解决方案


推荐阅读