简体   繁体   English

如何从WP REST API获取JSON数据

[英]How to get JSON data from WP REST API

For a long time I've being working on how to get access to my WordPress posts. 长期以来,我一直在研究如何访问我的WordPress帖子。 I'm using WP REST API plugin with the WP REST API - OAuth 1.0a Server, and I get an "401 - Only authenticated users can access the REST API" error. 我将WP REST API插件与WP REST API-OAuth 1.0a Server一起使用,并且收到“ 401-只有经过身份验证的用户才能访问REST API”错误。

I've read all the documentation and I touched the .htaccess , and I'm going crazy. 我已经阅读了所有文档,并且触摸了.htaccess ,我快要疯了。 Please someone help I don't know what to do, and I'm about to give up. 请有人帮忙,我不知道该怎么办,我将放弃。

I tried add some code in the .htaccess file, and also tried AJAX and JavaScript fetch, and nothing seems to work. 我尝试在.htaccess文件中添加一些代码,还尝试了AJAX和JavaScript提取,但似乎没有任何效果。 I know there are people using it. 我知道有人在用它。 It'd be nice someone can tell me how to fix this, I really need it. 有人可以告诉我如何解决此问题,这是很好,我真的很需要。

jQuery.ajax({
   url: 'http://laprensainsolita.com/wp-json/wp/v2/posts',
   method: 'GET',
   crossDomain: true,
   beforeSend: function ( xhr ) {
       xhr.setRequestHeader( 'Authorization', 'Basic ' + Base64.encode( 'username:password' ) );
   },
   success: function( data, txtStatus, xhr ) {
       console.log( data );
       console.log( xhr.status );
   }
});

Do I need to add any code to the .htaccess file? 我是否需要在.htaccess文件中添加任何代码? And if so what and where should I put it? 如果是这样,我应该放在哪里? Or what is need to make this work. 或需要什么来使这项工作。 I'm very desperate. 我很绝望。

The error: 错误:

{
  "code": "rest_cannot_access",
  "message": "Only authenticated users can access the REST API.",
  "data": {
    "status": 401
  }
}

Please try to pass fields to store credentials. 请尝试传递字段以存储凭据。 Add this fragment to your AJAX call 将此片段添加到您的AJAX调用中

xhrFields: {
    withCredentials: true
},

Your call should look something like this: 您的通话应如下所示:

Query.ajax({
 url: 'http://laprensainsolita.com/wp-json/wp/v2/posts',
 method: 'GET',
 crossDomain: true,
 xhrFields: {
    withCredentials: true
},
 beforeSend: function ( xhr ) {
   xhr.setRequestHeader( 'Authorization', 'Basic ' + Base64.encode( 
             'username:password' ) );
 },
   success: function( data, txtStatus, xhr ) {
   console.log( data );
   console.log( xhr.status );
 }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 WP Rest Api 的自定义端点获取 JSON 数据并将其应用于 Gutenberg 自定义块组件 - How to get JSON data from custom end point of WP Rest Api and apply it to Gutenberg custom block component 如何在coffeescript中执行HTTP GET以从REST API获取JSON - How to do a HTTP GET in coffeescript to get a JSON from a REST API 如何使用 jQuery 或纯 JavaScript 从 URL (REST API) 到 UI 获取 JSON 数据? - How to get JSON data from the URL (REST API) to UI using jQuery or plain JavaScript? 如何从JSON API URL获取数据? - How to get data from json api url? 如何在NodeJs REST API中从mysql获取特定数据 - How To get specific data from mysql in NodeJs REST API 如何使用 angularJS 自定义服务从 REST API 获取数据 - How to get data from REST API using angularJS custom service 如何从具有基本身份验证的rest api获取数据? - How to get data from rest api which has basic authentication? 如何将 JSON 数据传递到 Express REST API - How to Pass JSON Data into Express REST API ReactJS,试图从JIRA的REST API获取访问JSON数据,但无法这样做。 - ReactJS, trying to get access JSON data from REST API from JIRA, but unable to do so. 从 django-rest-api 获取 json 数据,将前端反应到变量中 - Get json data from from django-rest-api with react frontend into a variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM