在 Wordpress 中使用 Gravity Form API 的 CORS 问题

CORS issues using Gravity Form API in Wordpress

我最近一直在使用重力形式 API 从 wordpress 网站获取条目到 angular 应用程序。 angular 应用程序未部署在与 wordpress 网站相同的域中。因此我遇到了诸如 XMLHTTPRequest cannot load [...] No 'Access-Control-Allow-Origin header is present.

之类的问题

有没有人遇到过 Gravity Form API 的这个问题并且有解决这个 CORS 问题的方法(比如要包含什么?)?

使用 init 操作绑定新的 WP 函数,在 wordpress 中配置您的 CORS,例如:

add_action('init', 'handle_preflight');

function handle_preflight() {
    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
    header("Access-Control-Allow-Headers: Origin, Content-Type, Accept");

    if('OPTIONS' == $_SERVER['REQUEST_METHOD']) {
        status_header(200);
        exit();
    }
}

修改您的服务器以添加 header Access-Control-Allow-Origin: *