'Access-Control-Allow-Origin in wordpress

I add the following code to my function.php

function add_cors_http_header(){
    header("Access-Control-Allow-Origin: *");
}
add_action('init','add_cors_http_header');

But when i run my web application get the following error:

XMLHttpRequest error.


Solution 1:[1]

To fix Access-Control-Allow-Origin, you have to do it from .htaccess. Here is the code to add to that file as well as the reference article, which explains the error in more depth.

https://crunchify.com/how-to-fix-access-control-allow-origin-issue-for-your-https-enabled-wordpress-site-and-maxcdn/

<IfModule mod_headers.c>
  <FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$">
    Header set Access-Control-Allow-Origin "*"
  </FilesMatch>
</IfModule>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JayDev95