WordPress endpoint plugin shows SyntaxError: JSON.parse

WordPress endpoint plugin shows SyntaxError: JSON.parse

我阅读了很多页面以了解如何在我的简单 WP-Plugin 中创建一个简单的端点。

我为此阅读的好文章链接:

https://developers.shopware.com/developers-guide/rest-api/plugin-api-extension/ , https://wptips.dev/custom-rest-api/ , https://torquemag.io/2016/07/adding-custom-endpoints-extra-touches/ , https://www.cloudways.com/blog/wordpress-rest-api-to-fetch-posts/#get-wp-v2-posts , https://www.cloudways.com/blog/wordpress-rest-api-to-fetch-posts/#wordpress-rest-api-using-json , https://developer.wordpress.org/rest-api/

这给了我最大的成功希望,我使用了这里的来源:

我的 URL 我在 Web 浏览器中使用:

http://localhost/wordpress/wp-json/a0plugin/v1/testing

我从 gist 中完整来源的摘录

htdocs/wp-content/plugins/a0plugin/a0plugin.php

<?php
/**
 * Plugin Name:         a0plugin
 */
function at_rest_testing_endpoint(){
    return new WP_REST_Response('Howdy!!');
}
function at_rest_init(){
    $namespace = 'a0plugin/v1';
    $route     = 'testing';
    register_rest_route($namespace, $route, array(
        'methods'   => WP_REST_Server::READABLE,
        'callback'  => 'at_rest_testing_endpoint'
    ));
}
add_action('rest_api_init', 'at_rest_init');
?>

完整来源:

https://gist.github.com/sl5net/10d21e8bd358b9149968885a93862424

语法错误:JSON.parse

错误:SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

如果省略注释,错误就会消失。这些都没有列在所谓的相关源文本摘录中。我不知道原因。