使用POST方法返回禁止403(WP-REST-API)
问题描述:
我是全新的wordpress/php我正在尝试使用Angular和其他API来在定制表professor_schedule
上执行基本CRUID。使用POST方法返回禁止403(WP-REST-API)
要查询表格,一切正常。这是我的。
PHP
// get all schedules
function getAllProfessorSchedule($data) {
global $wpdb;
$query = "SELECT nom FROM `professor_schedule`";
$list = $wpdb->get_results($query);
return $list;
}
add_action('rest_api_init', function() {
register_rest_route('professor-schedule/v2', '/all', array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'getAllProfessorSchedule'
));
});
JS
function getAllSchedules(){
$http({
method: 'GET',
url : 'http://localhost/structurecours/index.php/wp-json/professor-schedule/v2/all'
}).then(function (response) {
console.log(response.data)
$scope.data = response.data
}, function (response) {
console.error("error !! ", response)
});
}
下面是将数据插入到数据库的代码。用403(禁止)
我登录使用管理员帐户服务器响应我曾尝试与Basic Auth plugin,但我总是得到403错误。我现在挣扎了好几个小时。 我会得到任何建议。由于
PHP
function addNewSchedule(WP_REST_Request $request) {
// $args = array(
// 'headers' => array(
// 'Authorization' => 'Basic ' . base64_encode('user:password'),
// ),
//);
// wp_remote_request($url, $args);
global $wpdb;
$item = $request->get_json_params();
$fields = array();
$values = array();
foreach($item as $key => $val) {
array_push($fields, $key);
array_push($values, $val);
}
$fields = implode(", ",$fields);
$values = implode("','",$values);
$query = "INSERT INTO `professor_schedule` (".$fields.") VALUES ('".$values."')";
//$query = "INSERT INTO `professor_schedule` ('Nom') VALUES ('test')";
$list = $wpdb->get_results($query);
return $list;
}
add_action('rest_api_init', function() {
register_rest_route('professor-schedule/v2', '/add', array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => 'addNewSchedule',
'permission_callback' => function() {
return current_user_can('edit_others_posts');
}
));
});
JS
$scope.addNewSchedule = function(){
$http({
method : "POST",
url : "http://localhost/structurecours/index.php/wp-json/professor-schedule/v2/add",
params:
{
nom : $scope.scheduleModel.nom
}
}).then(function(){
getAllSchedules();
});
}
答
我有同样的问题WordPress的端点。您可以尝试在服务器上禁用mod_security
,或者为REST端点设置mod_security
的例外。您也可以在本地服务器上尝试WordPress并确认问题。
这些片段中的哪一个是来自 的错误? –
addNewSchedule函数 – Merlin