I’m trying to figure out how I should call a REST API custom endpoint from the Javascript of a plugin. Here is my PHP code excerpt:

class IESRestEndpoint 
{
  public __construct()
  {
     add_action( 'rest_api_init', array(&$this, 'registerRoutes')); 
  }

  public function istermactive(WP_REST_Request $request)
  {
    return true; // this is my controller.
  }

  public function registerRoutes()
  {
    register_rest_route( 'ies/v1', 
                         '/istermactive/requestid=(?P<requestid>[a-zA-Z0-9-]+)/selectname=(?P<selectname>[a-zA-Z0-9-]+)/selectvalue=(?P<selectvalue>[a-zA-Z0-9-]+)', 
                          array('methods' => 'GET',
                                'callback' => array(&$this,
                                                    'istermactive')
                               )
                       );
  }
}

In order to enable REST API calls client side, I specify wp-api as dependency of my plugin JS script.

wp_register_script($handle, $jsfileurl, array("wp-api") );

And here is my JS code:

wp.api.loadPromise.then(function () 
{
  wp.api.namespace("ies/v1").istermactive().
                                      param(...

However this JS code throws an exception in the JS console after the call to loadPromise.then() and before it reaches the call to wp.api.namespace():

Uncaught TypeError: _.includes is not a function
    at wp-api.min.js?ver=4.7.4:1
    at Function.h.each.h.forEach (underscore-min.js?ver=4.7.4:1)
    at Object.wp.api.utils.decorateFromRoute (wp-api.min.js?ver=4.7.4:1)
    at wp-api.min.js?ver=4.7.4:1
    at Function.h.each.h.forEach (underscore-min.js?ver=4.7.4:1)
    at n.constructFromSchema (wp-api.min.js?ver=4.7.4:1)
    at n.<anonymous> (wp-api.min.js?ver=4.7.4:1)
    at n.<anonymous> (backbone.min.js?ver=1.2.3:1)
    at n.<anonymous> (underscore.min.js?ver=1.8.3:5)
    at _ (backbone.min.js?ver=1.2.3:1)

My JS file (ies.js) is not even mentioned in the stack trace. What am I missing?

Read more here: REST API: Backbone and custom endpoint


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: