gpt4 book ai didi

wordpress - 从本地主机到rest api结果的认证CORS错误

转载 作者:行者123 更新时间:2023-12-03 08:11:59 24 4
gpt4 key购买 nike

我刚刚在 dev server 上安装了一个新的 wp .我正在尝试使用位于我的计算机/设备上的 ionic/angularjs 应用程序对其余 api(插件,因为它是 wp v.4.6.3)进行身份验证。当前使用 JWT Authentication for WP-API插入。
这是我的 header ,在 HTTP header 插件的帮助下:

content-encoding: gzip
x-powered-by: php/5.5.9-1ubuntu4.21
connection: keep-alive
content-length: 3361
keep-alive: timeout=5, max=95
access-control-allow-headers: accept, authorization, cache-control, cookie, content-type, origin
server: apache/2.4.7 (ubuntu)
x-frame-options: allow-from *
vary: accept-encoding
access-control-allow-methods: get, post, options, head, put, delete, trace, connect, patch
content-type: text/html; charset=utf-8
access-control-allow-origin: *
access-control-expose-headers: cache-control, cookie, content-type, origin
cache-control: no-cache, must-revalidate, max-age=0

无论我做什么,我都会收到一些 CORS 错误。最新的是:

Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response.



JWT 插件在文档中也提到了编辑 .htaccess 和 wp-config.php,我就是这样做的。尝试了 htacces 编辑和/或插件的几种组合。但是会弹出相同或类似的错误。

这是我的代码,基于 JWT 插件的文档(凭证/网址有效!):
var apiHost = 'http://dev.imok.ro/authworks/wp-json';
$http.post( apiHost + '/jwt-auth/v1/token', {
username: 'admin',
password: 'admin!@#'
})
.then( function( response ) {
console.log( 'siker', response.data )
})
.catch( function( error ) {
console.error( 'Errorrrr', error );
});

.htaccess:
#<ifModule mod_headers.c>
# Header always set Access-Control-Allow-Origin: *
## Header always set Access-Control-Allow-Methods "POST, GET, PUT, DELETE, OPTIONS"
## Header always set Access-Control-Allow-Headers "content-type"
#</ifModule>

<IfModule mod_rewrite.c>

#SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1

RewriteEngine on
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]

RewriteEngine On
RewriteBase /authworks/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /authworks/index.php [L]
</IfModule>

谢谢你的帮助!
玛格

最佳答案

你可以通过编辑 public/class-jwt-auth-public.php 来解决这个问题

public function add_cors_support()
{
$enable_cors = defined('JWT_AUTH_CORS_ENABLE') ? JWT_AUTH_CORS_ENABLE : false;
if ($enable_cors) {
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: GET, POST, UPDATE, DELETE' );
header( 'Access-Control-Allow-Headers: Authorization, Content-Type' );
header( 'Access-Control-Allow-Credentials: true' );
return $value;
});

// $headers = apply_filters('jwt_auth_cors_allow_headers', 'Access-Control-Allow-Headers, Content-Type, Authorization');
// header(sprintf('Access-Control-Allow-Headers: %s', $headers));
}
}

我已提交 PR

关于wordpress - 从本地主机到rest api结果的认证CORS错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42253827/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com