我正在使用ionic 3框架,我想从wordpress的服务发布类型中获取一些数据。 这是我在wordpress中的自定义端点。
<?php
add_action( 'rest_api_init', function () {
register_rest_route( 'my-service', '/v1', array(
'methods' => 'POST',
'callback' => 'my_services',
) );
} );
function my_services( WP_REST_Request $request ) {
$args = array(
'post_type' => 'services',
'post_status' => array('publish'),
'meta_query' => array(
array(
'key' => 'service_title',
'value' => $request['data']['title'],
'compare' => 'LIKE'
)
)
);
$posts = get_posts( $args );
$data = array();
foreach ($posts as $post) {
$services = array();
$services['id'] = $post->ID;
$services['title'] = get_field('service_title', $post->ID);
$services['description'] = get_field('service_description', $post->ID);
$data[] = $services;
}
return $data;
}
这是来自ionic的代码,用于访问自定义端点
import { Component } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
@Component({
selector: 'page-service',
templateUrl: 'service.html',
})
export class ServicePage {
constructor(
private http: Http
) {
}
ionViewDidLoad() {
this.http.post('https://mywebsite.com/wp-json/wp/v2/my-service/v1', { title: 'Some Service Title' })
.map(res => res.json()).subscribe( data => {
console.log(data)
}, err => {
console.log(err);
});
}
}
这在使用xampp的localhost上正常工作。但是,当我将其迁移到实时服务器时,会发生错误。
它返回OPTIONS https://mywebsite.com/wp-json/wp/v2/my-service/v1 0 ()
这些是标题返回
如何在WP Rest API中访问POST端点?
在本地服务器上,它工作正常。现场直播并非如此。我是否错过了有关HTTP请求的实时服务器上的配置?