Search code examples
angularjswordpressgravity-forms-plugin

CORS issues using Gravity Form API in Wordpress


I've been using gravity form APIs lately to get entries from a wordpress website to an angular app. The angular app is not deployed in the same domain as the wordpress website. Thus I'm having cors issues like XMLHTTPRequest cannot load [...] No 'Access-Control-Allow-Origin header is present.

Has somewone ever faced this with Gravity Form APIs and have a solution for this CORS issue (like what to include ?) ?


Solution

  • Configure your CORS in wordpress by using the init action to bind a new WP-Function like:

    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();
        }
    }