• Resolved Kevin

    (@truheart)


    So far I have been having fun with the REST API while at the same time running into a couple of frustrations.

    So in this specific use case, I have a form on the frontend trying to pass data to a custom endpoint once user submits via the javascript Fetch API.

    Javascript

    
    fetch( this.route + '?account_info=' + account_changes, {
    
        method: 'POST'
    
    } )
    .then( function( fetch_response ) {
    
        // Success 
        if( response.ok ) {
    
            console.log( fetch_response );
    
        }
        // Error
        else {
    
            console.log( 'Response Error', response );
    
        }
    
    } )
    .then( function( wp_response ) {
    
        console.log( wp_response );
    
    } );
    

    Here I am passing all of the form data as a javascript object assigned to the account_changes variable. The fetch_response is showing up fine, giving me a 200 status which means that the endpoint is available, and I am assuming my issue has to do on the WordPress end of things.

    PHP

    
    class API extends \WP_REST_Controller {
     
        /**
         * Register the routes for the objects of the controller.
         */
        public function register_routes() {
    
            register_rest_route( Custom_Class::$rest_namespace, '/users', [
    
                [
                    'methods'             => WP_REST_Server::EDITABLE,
                    'callback'            => [ $this, 'update_user' ],
                    'show_in_index'       => false,
                    'args'                => [],
                ]
        
            ] );
        }
    
        /**
         * Update user across all sites
         */
        public function update_user( $request ) {
    
           return 'heeey';
    
        }
    
    }
    

    Here is the relevant code in question. First, don’t worry about Custom_Class::$rest_namespace placeholder. I just inserted it for here. The actual variable that goes here is working fine. From the docs I see that WP_REST_Server::EDITABLE should be work with POST requests to the endpoint and run the callback, but I can’t get it to work. In theory the wp_response in the fetch call should be showing the ‘heeey’ returned by the callback function, but instead it is showing up as undefined.

    I’ve tried a bunch of different variations with no success. I’m also not sure how authentication is supposed to work here, but figured if that was the issue then it would be showing up within the server_response.

    Anyway hopefully this is a good place to get some advice. Thanks.

    • This topic was modified 8 years, 9 months ago by Kevin.
    • This topic was modified 8 years, 9 months ago by Kevin.
Viewing 1 replies (of 1 total)
  • Thread Starter Kevin

    (@truheart)

    Sorry, looks like it actually was my javascript. I found this little course preview from Zac Gordon that helped alot Decoupled WordPress with Vanilla JavaScript and Fetch

    
    fetch( this.route + '?account_info=' + account_changes, {
    
        method: 'POST'
    
    } )
    .then( function( response ) {
    
        // Error
        if( response.status !== 200 ) {
    
            console.log( 'Problem! ', response );
    
        }
        // Success 
        response.json().then( function( wp_response ) {
    
            console.log( wp_response );
    
        } );
    
    } )
    .catch( function( err ) {
    
        console.log( 'Server Error: ', err );
    
    } );
    
    • This reply was modified 8 years, 9 months ago by Kevin.
Viewing 1 replies (of 1 total)

The topic ‘WP REST API Custom Endpoint with EDITABLE method not returning values’ is closed to new replies.