简体   繁体   中英

Wordpress custom endpoints (WP_REST_Controller) 404 only on mobile

I currently have a working controller that extends WP_REST_Controller in a file under the current theme. These are being called using jQuery ajax. (all code below)

The issue I am facing is that I receive this error ONLY when accessing with a mobile device.

{"code": "rest_no_route", "message": "No route was found matching the URL and request method" "data": {"status": 404}}
  • settings -> permalinks -> save changes
  • tried using controller namespace "api/v1" and "wp/v2"

javascript

    function getAllClients() {
      jQuery.ajax({
        url: "http://myurl.com/index.php/wp-json/wp/v2/get_all_clients",
        type: "GET",
        data: { /*data object*/},
        success: function (clientList) {
          // success stuff here
        },
        error: function (jqXHR, textStatus, errorThrown) {
          alert(jqXHR.statusText);
        }
      })
    }

api/base.php

<?php

class ApiBaseController extends WP_REST_Controller
{
    //The namespace and version for the REST SERVER
    var $my_namespace = 'wp/v';
    var $my_version = '2';
    public function register_routes()
    {
        $namespace = $this->my_namespace . $this->my_version;

        register_rest_route(
            $namespace,
            '/get_all_clients',
            array(
                array(
                    'methods'  => 'GET',
                    'callback' => array(new ApiDefaultController('getAllClients'), 'init'),
                )
            )
        );


$ApiBaseController = new ApiBaseController();
$ApiBaseController->hook_rest_server();

api/func.php

<?php

class ApiDefaultController extends ApiBaseController
{
    public $method;
    public $response;


    public function __construct($method)
    {
        $this->method = $method;
         $this->response = array(
        //     'Status' => false,
        //     'StatusCode' => 0,
        //     'StatusMessage' => 'Default'
        // );
    }

    private $status_codes = array(
        'success' => true,
        'failure' => 0,
        'missing_param' => 150,
    );

    public function init(WP_REST_Request $request)
    {
        try {
            if (!method_exists($this, $this->method)) {
                throw new Exception('No method exists', 500);
            }
            $data = $this->{$this->method}($request);
            $this->response['Status'] = $this->status_codes['success'];
            $this->response['StatusMessage'] = 'success';
            $this->response['Data'] = $data;
        } catch (Exception $e) {
            $this->response['Status'] = false;
            $this->response['StatusCode'] = $e->getCode();
            $this->response['StatusMessage'] = $e->getMessage();
        }

        return $this->response['Data'];
    }

    public function getAllClients()
    {
       // db calls here
return json_encode($stringArr,true);
    }
}

These are registered in the Functions.php file

require get_parent_theme_file_path('api/base.php');
require get_parent_theme_file_path('api/func.php');

Turns out the issue was a plugin my client installed called "oBox mobile framework" that was doing some weird routing behind the scenes. Disabling it resolved the issue, though there is probably a way to hack around this and get both to play together.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM