You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VueJS 2调用WordPress/WooCommerce API遇CORS错误求助

本地VueJS 2调用WooCommerce REST API遇CORS预请求失败问题

本地通过vue-cli-service运行VueJS 2应用,使用axios调用WordPress/WooCommerce REST API时,始终出现以下CORS错误:

Access to XMLHttpRequest at 'https://woocommercewebsite.com/wp-json/wc/v3/products/categories/' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

目前可以正常调用其他REST API,但唯独WooCommerce的API无法调用。已尝试以下两种解决方案,但均无效:

尝试方案1:在WordPress主题functions.php中添加CORS头部

function add_cors_http_header(){
    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
    header("Access-Control-Allow-Credentials: true");
    header('Access-Control-Allow-Headers: Origin, X-Requested-With, X-WP-Nonce, Content-Type, Accept, Authorization');
}
add_action('init','add_cors_http_header');

尝试方案2:在Nginx虚拟主机配置文件中添加CORS头部

add_header "Access-Control-Allow-Origin" "*";
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, PATCH, DELETE, OPTIONS';

在Postman中可以看到响应已包含设置的CORS头部,但错误依然存在。已尝试StackOverflow上的相关方案,均未解决问题。了解过代理方案,但由于需要调用多个不同的REST API,且服务器为远程主机,该方案不适用。

内容的提问来源于stack exchange,提问作者John

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 21:50:20