[英]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.