I have this wordpress site with a plugin called JSON API. This plugin provides a JSON format for the content that is in the wordpress. I was able to enable CORS on the wordpress by adding header("Access-Control-Allow-Origin: *"); on the php header. But when I tried the url that the JSON API plugin provides the CORS does not work anymore.
我有一个带有一个名为JSON API的插件的wordpress网站。此插件为wordpress中的内容提供JSON格式。我能够通过添加标题(“Access-Control-Allow-Origin:*”)在wordpress上启用CORS;在PHP标题上。但是当我尝试使用JSON API插件提供的URL时,CORS不再起作用了。
This is the wordpress site were I'm doing the tests... I used the test cors website to check if it was working and it is... http://kiwa-app.loading.net/
这是wordpress网站,我正在做测试...我使用测试cors网站检查它是否正常工作,它是... http://kiwa-app.loading.net/
But when I try with the url that the JSON api provides me, is not working anymore. I'm still have the error No 'Access-Control-Allow-Origin' http://kiwa-app.loading.net/?json=info
但是,当我尝试使用JSON api为我提供的URL时,不再工作了。我仍然有错误No'Access-Control-Allow-Origin'http://kiwa-app.loading.net/?json=info
I will apreciate some help thanks!!!
我会感谢一些帮助!
5 个解决方案
#1
16
Ok I finally figured out an easy way...
好吧,我终于找到了一个简单的方法......
You just have to add:
你只需要添加:
<? header("Access-Control-Allow-Origin: *"); ?>
On the file api.php, this file is located in wp-content/plugins/json-api/singletons/api.php
在文件api.php上,此文件位于wp-content / plugins / json-api / singletons / api.php中
I hope it helps more people with the same problem!
我希望它可以帮助更多人解决同样的问题!
#2
11
I've used a few different WordPress API's - but for those of you using the 'official' WP-API, I had much trouble with this CORS --- and what I found was that between the .htaccess approach and a few others I stumbled upon... adding this to your theme functions.php worked best.
我使用了一些不同的WordPress API - 但是对于那些使用'官方'WP-API的人来说,我在使用这个CORS时遇到了很多麻烦 - 而我发现的是.htaccess方法和其他一些方法之间我偶然发现...将此添加到您的主题functions.php工作得最好。
function add_cors_http_header(){
header("Access-Control-Allow-Origin: *");
}
add_action('init','add_cors_http_header');
Be sure not to use any combinations of these ( .htaccess, header.php, api.php, functions.php ) as it will be angry at you.
一定不要使用这些的任何组合(.htaccess,header.php,api.php,functions.php),因为它会对你生气。
#3
7
Before the response is sent to the browser, we can run two action hooks and insert a new header()
:
在将响应发送到浏览器之前,我们可以运行两个操作挂钩并插入一个新的header():
do_action("json_api", $controller, $method);
do_action("json_api-{$controller}-$method");
The first one runs on every method, and the second one is to target specific methods. Here's an implementation of the first one, with a commented way to find the second:
第一个是在每个方法上运行,第二个是针对特定方法。这是第一个的实现,用注释的方式找到第二个:
add_action( 'json_api', function( $controller, $method )
{
# DEBUG
// wp_die( "To target only this method use <pre><code>add_action('$controller-$method', function(){ /*YOUR-STUFF*/ });</code></pre>" );
header( "Access-Control-Allow-Origin: *" );
}, 10, 2 );
#4
2
In wordpress goto plugins > JSON API > Edit
在wordpress goto插件> JSON API>编辑
From the right hand file selection select
从右侧文件选择中选择
json-api/singletons/api.php
JSON-API /单身/ api.php
You will need to add the following line
您需要添加以下行
header("Access-Control-Allow-Origin: *");
header(“Access-Control-Allow-Origin:*”);
Your code should look similar to this once done. Adding this line anywhere else might not work as expected.
一旦完成,您的代码应该与此类似。在其他地方添加此行可能无法按预期工作。
<?php
header("Access-Control-Allow-Origin: *");
class JSON_API {
function __construct() {
$this->query = new JSON_API_Query();
$this->introspector = new JSON_API_Introspector();
$this->response = new JSON_API_Response();
add_action('template_redirect', array(&$this, 'template_redirect'));
add_action('admin_menu', array(&$this, 'admin_menu'));
add_action('update_option_json_api_base', array(&$this, 'flush_rewrite_rules'));
add_action('pre_update_option_json_api_controllers', array(&$this, 'update_controllers'));
}
function template_redirect() {
#5
0
For anyone who is having this issue with multiple origins
对于任何有多个来源的问题的人
In your server hosting your wordpress site, navigate to ../wp-content/plugins/json-rest-api and from here open the plugin.php file.
在托管wordpress站点的服务器中,导航到../wp-content/plugins/json-rest-api,然后打开plugin.php文件。
In this function
在这个功能
function json_send_cors_headers( $value ) {..}
Change the header
更改标题
header( 'Access-Control-Allow-Origin: ' . esc_url_raw( $origin ) );
To
至
header( 'Access-Control-Allow-Origin: *' );
Hope this helps anyone who was incurring the same issues as I.
希望这有助于任何引发与我相同问题的人。
#1
16
Ok I finally figured out an easy way...
好吧,我终于找到了一个简单的方法......
You just have to add:
你只需要添加:
<? header("Access-Control-Allow-Origin: *"); ?>
On the file api.php, this file is located in wp-content/plugins/json-api/singletons/api.php
在文件api.php上,此文件位于wp-content / plugins / json-api / singletons / api.php中
I hope it helps more people with the same problem!
我希望它可以帮助更多人解决同样的问题!
#2
11
I've used a few different WordPress API's - but for those of you using the 'official' WP-API, I had much trouble with this CORS --- and what I found was that between the .htaccess approach and a few others I stumbled upon... adding this to your theme functions.php worked best.
我使用了一些不同的WordPress API - 但是对于那些使用'官方'WP-API的人来说,我在使用这个CORS时遇到了很多麻烦 - 而我发现的是.htaccess方法和其他一些方法之间我偶然发现...将此添加到您的主题functions.php工作得最好。
function add_cors_http_header(){
header("Access-Control-Allow-Origin: *");
}
add_action('init','add_cors_http_header');
Be sure not to use any combinations of these ( .htaccess, header.php, api.php, functions.php ) as it will be angry at you.
一定不要使用这些的任何组合(.htaccess,header.php,api.php,functions.php),因为它会对你生气。
#3
7
Before the response is sent to the browser, we can run two action hooks and insert a new header()
:
在将响应发送到浏览器之前,我们可以运行两个操作挂钩并插入一个新的header():
do_action("json_api", $controller, $method);
do_action("json_api-{$controller}-$method");
The first one runs on every method, and the second one is to target specific methods. Here's an implementation of the first one, with a commented way to find the second:
第一个是在每个方法上运行,第二个是针对特定方法。这是第一个的实现,用注释的方式找到第二个:
add_action( 'json_api', function( $controller, $method )
{
# DEBUG
// wp_die( "To target only this method use <pre><code>add_action('$controller-$method', function(){ /*YOUR-STUFF*/ });</code></pre>" );
header( "Access-Control-Allow-Origin: *" );
}, 10, 2 );
#4
2
In wordpress goto plugins > JSON API > Edit
在wordpress goto插件> JSON API>编辑
From the right hand file selection select
从右侧文件选择中选择
json-api/singletons/api.php
JSON-API /单身/ api.php
You will need to add the following line
您需要添加以下行
header("Access-Control-Allow-Origin: *");
header(“Access-Control-Allow-Origin:*”);
Your code should look similar to this once done. Adding this line anywhere else might not work as expected.
一旦完成,您的代码应该与此类似。在其他地方添加此行可能无法按预期工作。
<?php
header("Access-Control-Allow-Origin: *");
class JSON_API {
function __construct() {
$this->query = new JSON_API_Query();
$this->introspector = new JSON_API_Introspector();
$this->response = new JSON_API_Response();
add_action('template_redirect', array(&$this, 'template_redirect'));
add_action('admin_menu', array(&$this, 'admin_menu'));
add_action('update_option_json_api_base', array(&$this, 'flush_rewrite_rules'));
add_action('pre_update_option_json_api_controllers', array(&$this, 'update_controllers'));
}
function template_redirect() {
#5
0
For anyone who is having this issue with multiple origins
对于任何有多个来源的问题的人
In your server hosting your wordpress site, navigate to ../wp-content/plugins/json-rest-api and from here open the plugin.php file.
在托管wordpress站点的服务器中,导航到../wp-content/plugins/json-rest-api,然后打开plugin.php文件。
In this function
在这个功能
function json_send_cors_headers( $value ) {..}
Change the header
更改标题
header( 'Access-Control-Allow-Origin: ' . esc_url_raw( $origin ) );
To
至
header( 'Access-Control-Allow-Origin: *' );
Hope this helps anyone who was incurring the same issues as I.
希望这有助于任何引发与我相同问题的人。