0

我正在尝试通过 javascript (ReactJS) 向 Wit.ai 发出 API 请求。我的浏览器网络选项卡显示呼叫失败并显示以下消息:

“错误”:“错误的身份验证,检查令牌/参数”

但是,同一调用在 Wit.ai 日志中显示为成功。我已验证凭据正确,并且可以通过终端成功 cUrl 通话。

这是电话:

async action() {
    const resp = await fetch('https://api.wit.ai/message?v=20160526&q=hello', {
      method: 'GET',
      headers: {
        'Authorization': "Bearer " + accessToken
      },
      dataType: 'jsonp',
      mode: 'no-cors',
      credentials: 'include'
    }).then(resp => resp.json()).catch(e => console.log('Boo', e));
}
4

1 回答 1

0

由于它是 JSONP 请求,即使请求已正确执行,您也会遇到“意外的输入结束”。我不确定是否有任何方法可以在不通过您的应用服务器代理请求的情况下使其工作。无论如何,对于这种请求,请headers完全删除并将访问令牌作为access_token查询参数移动到查询字符串中:

await fetch(`https://api.wit.ai/message?v=20160526&q=hello&access_token=${accessToken}`, {
  method: 'GET',
  dataType: 'jsonp',
  mode: 'no-cors',
  credentials: 'include'
}).then(resp => resp.json()).catch(e => console.log('Boo', e));

查看浏览器上的“网络”选项卡以查看请求是否已成功解决,即使在执行此调用catch期间已到达块。fetch

于 2016-09-24T14:26:03.380 回答