【发布时间】:2016-09-17 20:14:58
【问题描述】:
对使用“wp rest api v2”的我的 WordPress 应用程序的本地主机请求按预期工作,没有问题。这是我的本地主机发布请求的示例:
POST "http://127.0.0.1/plugin namespace/plugin-name/wp-json/plugin namespace/api url 的其余部分"
但在我的托管站点上,请求返回一个错误,与我的托管站点类似的请求:
POST "http://my-domain.com/plugin-name/wp-json/plugin 命名空间/api url 的其余部分"
然后我进入 chrome 控制台“400(错误请求)” 对象{代码:“rest_invalid_param”,消息:“无效参数:id”,数据:对象}
另一个注意,两个请求中的有效负载是相同的:
$http.post(url, { id : 1, shortMessage:"a b c"}, {headers: {'X-WP-Nonce': "我的 nonce 值"}}); // 我正在使用 angular $http 来发出 post 请求
我在上面写了 url 的样子。
在我的插件 php 代码中我写了:
function myplugin_register_endpoints(){
register_rest_route(
'plugin namespace',
'/rest of the api url',
array(
'methods' => 'POST',
'callback' => 'my_end_point',
'args' => array(
'id' => array(
'required' => true,
'validate_callback' => function($param, $request, $key) {
return is_numeric( $param ) and ! is_null(get_post($param));//numeric post id value and there is valid post for this id
},
'sanitize_calback' => 'absint'
)
),
'permission_callback' => function (WP_REST_Request $request) {
if(!is_user_logged_in()){
return new WP_Error('login error',__('You are not logged in','blabla'));
}
return true;
}
)
);
}
add_action('rest_api_init','myplugin_register_endpoints');
function my_end_point(WP_REST_Request $request) {
global $current_user;
$current_user = wp_get_current_user();
if($my_var){
return array('message' => $message,'items' => $items,'item'=>$item);
} else {
return new WP_Error('add friend error',__('message'),$request['id']);
}
}
我需要了解为什么我的托管网站上的请求失败。
谢谢 克
【问题讨论】:
标签: angularjs wordpress api rest