Set cors header for ajax requests

I am trying to use wp-ajax inside another sub-domain, but I am getting a CORS error. To solve this I used add_action with the send_headers hook

function cors_header () {     header('Access-Control-Allow-Origin: *'); } add_action('send_headers', 'cors_header'); 

But this didn’t work either. This is all my code.

    $  email = $  _POST['email'];     if (!$  email) {         status_header(401, 'Unauthorized Request');         exit();     }     $  form_id = 25;     $  search_criteria = array(         'status'     => 'active',         'field_filters' => array(             array(                 'key' => '2', 'value' => $  email,             )         )     );          $  entries = GFAPI::get_entries($  form_id, $  search_criteria);          add_action('send_headers', 'cors_header');          echo json_encode($  entries[0]['created_by']);     exit(); }  function cors_header () {     header('Access-Control-Allow-Origin: *'); } add_action("wp_ajax_nopriv_user_id", "get_user_id"); add_action("wp_ajax_user_id", "get_user_id");