Quantcast
Channel: WordPress.org Forums » All Topics
Viewing all articles
Browse latest Browse all 67836

REST API Custom Endpoint – Problem CORS?

$
0
0

Replies: 0

Hi everybody,

We are developing an app on ionic2, consuming data from a custom endpoint on REST API for WP.

The thing is, if we use the endpoints provided for WordPress REST API plugin, there is no problem, but if we create our custom endpoint, javascript from ionic2 get 404, like a problem with CORS.

Of course, if we request the same URL by any navigator (chrome, firefox, etc) we have no problem.

Any ideas? We have tried we WP CORS for example, but nothings happens

The code for the custom endpoint is like that:

register_rest_route('mytheme/v1', '/postsfortag/(?P<tagid>(.*)+)', array(
        'methods' => 'GET',
        'callback' => 'get_posts_by_tag',
    ) );

function get_posts_by_tag($data){
    global $wpdb;
    $tagId = $data['tagid'];

    $args = array('tag_id' => $tagId, 'posts_per_page' => 10);

    $posts = get_posts($args);

    $resultJSON = array();
    $i = 0;
    foreach ($posts as $post){
        $postId = $post->ID;
        $resultJSON[$i]["id"] = $postId;
        $resultJSON[$i]["post_title"] = $post->post_title;
        $resultJSON[$i]["post_date"] = $post->post_date;
        $resultJSON[$i]["guid"] = get_permalink($postId);
        $resultJSON[$i]["image"] = getPostThumbnail($postId);
        $resultJSON[$i]["author"] = getPostAuthor($post);

        $i++;
    }

    return $resultJSON;
}

Thanks in advance!


Viewing all articles
Browse latest Browse all 67836

Trending Articles