2017-06-18 15 views
0

私は完全にWordPress/PHPには新機能です。残りのAPIでAngularを使用してカスタムテーブル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) 
    }); 
} 

以下DBにデータを挿入するためのコードです。サーバーは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(); 
    }); 
} 
+0

のエラーはどれですか? –

+0

addNewSchedule関数 – Merlin

答えて

0

は、私は同じ問題のWordPressのエンドポイントを持っていました。サーバー上でmod_securityを無効にするか、RESTエンドポイントに対してmod_securityの例外を試すことができます。また、ローカルサーバー上でWordPressを試して問題を確認することもできます。

関連する問題