【问题标题】:javascript fetching an API json rest cannot access the datajavascript获取API json rest无法访问数据
【发布时间】:2019-07-26 14:59:45
【问题描述】:

这太疯狂了,我有一个 API json 休息,我可以从 firefox 扩展 RESTED 完美访问它,并且直接从浏览器我可以看到数据,但是当我尝试我的代码时它说“代码:”rest_cannot_access”,消息: “只有经过身份验证的用户才能访问 REST API。”。所以我不明白为什么。

这是一个 wordpress rest api 插件,我试图从 fetch javascript 中获取数据。这是 url http://laprensainsolita.com/wp-json/wp/v2/posts

这是我要测试的代码。

    .then(response => {
      return response.json()
    })
    .then(data => {
      console.log(data)
    })
    .catch(err => {
      console.log(data)
    })

【问题讨论】:

  • 我无法直接从浏览器访问此信息,我也收到身份验证错误。因此,api 必须要求凭据与请求一起发送。您可能在浏览器上为该网站保存了一个身份验证 cookie。
  • @CalIrvine 是的,卡尔你说得对,我刚刚检查了其他电脑,我得到了同样的错误“无法访问”。那么我是否需要一个 auth wordpress 插件才能访问(GET)?因为'我不想做任何帖子或删除,我只想获取数据。谢谢
  • 我不知道 WP rest api 的细节,但也许这个wordpress.stackexchange.com/questions/265523/… stackexchange 问题会有所帮助?祝你好运。

标签: javascript json wordpress api fetch


【解决方案1】:

我想你应该看看这里https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/

确保您通过 cookie 登录。尝试打开一个“私人浏览器窗口”,你会得到同样的错误。

【讨论】:

  • 非常感谢
猜你喜欢
  • 2018-10-25
  • 1970-01-01
  • 1970-01-01
  • 2016-10-26
  • 2017-08-18
  • 1970-01-01
  • 2019-08-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多