【发布时间】:2017-07-04 09:02:06
【问题描述】:
我刚刚在dev server 上安装了一个新的 wp。我正在尝试使用位于我的计算机/设备上的 ionic/angularjs 应用程序对其余 api(插件,因为它是 wp v.4.6.3)进行身份验证。目前使用JWT Authentication for WP-API 插件。 这是我的标头,在 HTTP 标头插件的帮助下:
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 错误。最近的是:
请求标头字段 Content-Type 不允许 预检响应中的 Access-Control-Allow-Headers。
JWT 插件在文档中也提到了编辑 .htaccess 和 wp-config.php,我这样做了。尝试了 htacces 编辑和/或插件的几种组合。但是会弹出相同或类似的错误。
这是我的代码,基于 JWT 插件的文档(凭证/url 有效!):
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>
感谢您的帮助! 马戈尔
【问题讨论】:
-
对我来说同样的问题
标签: wordpress .htaccess ionic-framework cors wordpress-rest-api