3

I cannot load admin-ajax.php and I keep getting this error message:

XMLHttpRequest cannot load ..../wp-admin/admin-ajax.php.,. No 'Access-Control-Allow-Origin' header is present on...

On local WAMP it worked just to add this:

header("Access-Control-Allow-Origin: *"); 

(Even if this seems very stupid because next time WordPress updates I guess this would disappear.)

When I upload this to my production server it's still the same message:

XMLHttpRequest cannot load ..../wp-admin/admin-ajax.php.,. No 'Access-Control-Allow-Origin' header is present on...

I've tried to modify the .htaccess file and that seemed to activate CORS, but that won't affect admin-ajax.php:

<IfModule mod_headers.c>
   Header add Access-Control-Allow-Origin: *
</IfModule>

I've also tried to install WP-CORS plugin without success.

2 Answers 2

10

There are filters for allowed_http_origins and add_allowed_origins. You can use them to set the proper Access-Control-Allow-Origin header in the response to your AJAX call.

Add this to your theme's functions.php file:

add_filter('allowed_http_origins', 'add_allowed_origins');

function add_allowed_origins($origins) {
    $origins[] = 'https://www.yourdomain.com';
    return $origins;
}
2
  • 4
    Did not work for me. I still get the same error.
    – Amjad
    Commented May 10, 2018 at 16:38
  • i have used all method but still i can't resolve my same issue. If you have another method please share. Thanks Commented May 16, 2022 at 16:56
0

Add this to your .htaccess file:

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
</IfModule>

Not the answer you're looking for? Browse other questions tagged or ask your own question.