Lalit Dogra
Lalit Dogra

Reputation: 11

Wordpress Api custom api is not working

i am working with ionic2 and wordpress api and facing this error XMLHttpRequest cannot load http://uniquecoders.in/dev/videogallery/wp-json/wp/v2/sp_html5video. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.

Upvotes: 0

Views: 596

Answers (2)

Lakshmi Narasimhan
Lakshmi Narasimhan

Reputation: 159

Try adding the code in wordpress htaccess file

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
</IfModule>

and add following action in wordpress

/**
 * Use * for origin
 */
add_action( 'rest_api_init', function() {

    remove_filter( 'rest_pre_serve_request', 'rest_send_cors_headers' );
    add_filter( 'rest_pre_serve_request', function( $value ) {
        header( 'Access-Control-Allow-Origin: *' );
        header( 'Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE' );
        header( 'Access-Control-Allow-Credentials: true' );

        return $value;

    });
}, 15 );

or simply add the following in wp-content/plugins/json-api/singletons/api.php

 <? header("Access-Control-Allow-Origin: *"); ?>

Upvotes: 1

Crisoforo Gaspar
Crisoforo Gaspar

Reputation: 3830

This is basically a CORS issue not the from the API since you are trying to access from a different origin.

You have a coupple of great answers over here to enable access from a specific origin or any origin, (since you can specify a single origin or any origin).

Upvotes: 0

Related Questions