Unable to create a Rest end-point

Question

I’m tring to make an Ajax call in my plugin.
My front-end code tryes to call Rest my POST API wp-json/imp/search-cert

<script>
    var app = angular.module('app', []);
    app.controller('AppCtrl', function (\$scope,\$http ){
        \$scope.valore="pippo";
        \$scope.cerca = function(){
            \$http({
                method: 'POST',
                url: './wp-json/imp/search-cert',
                data: \$scope.query
            }).then(function (response) {
                \$scope.valore="pluto";   
            }, function (error, status) {
                //\$scope.show_error_message('Errore', error.data.message);
            });   

        }
    });

</script>

I registered the API with this code

function read_certificate_func( $data ) {
    $posts = get_posts( array(
        'certId' => $data['certId'],
    ) );

    if ( empty( $posts ) ) {
        return null;
    }

    return $posts[0]->post_title;
}

add_action( 'rest_api_init', function () {
    register_rest_route( 'imp',
        '/search-cert',
        array(
            'methods' => 'POST',
            'callback' => 'read_certificate_func',
    ) );
} );    

but when I invoke the API wordpress replays with 404 error
I’ve tried to put a break-point inside read_certificate_func but the function in not processed at all.

0
Roberto Petrilli 2 weeks 2022-07-25T17:56:57-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse