1

I am pulling posts from one WordPress website to another. Both websites are on same server. For pulling posts i am using jQuery.ajax with Rest API. But i am getting error message in console

Access to XMLHttpRequest at 'https://www.website1.com/wp-json/wp/v2/posts/?tags=4,198&per_page=24&offset=1&_embed' from origin 'https://website2.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

For this i have added following header

Header set Access-Control-Allow-Origin "*"

In .htaccess of both websites. Also i have used this

crossDomain: true,

In ajax. But still having error. Any help. Please

fzaman
  • 17
  • 2
  • 7
  • 1
    Can you inspect the headers of the response (of jQuery.ajax) and see `Access-Control-Allow-Origin: *` is there? – ibex Jan 27 '20 at 21:48
  • I think the following link can help you. **https://stackoverflow.com/questions/20035101/why-does-my-javascript-code-get-a-no-access-control-allow-origin-header-is-pr** – saman Jan 27 '20 at 22:25

1 Answers1

0

Just add below lines to .htaccess file and we should be good.

<ifmodule mod_headers.c="">
   SetEnvIf Origin "^(.*\.domain\.com)$" ORIGIN_SUB_DOMAIN=$1
   Header set Access-Control-Allow-Origin "%{ORIGIN_SUB_DOMAIN}e" env=ORIGIN_SUB_DOMAIN
   Header set Access-Control-Allow-Methods: "*"
   Header set Access-Control-Allow-Headers: "Origin, X-Requested-With, Content-Type, Accept, Authorization"
</ifmodule>
PKInd007
  • 388
  • 2
  • 7
  • Not working. I have change this line SetEnvIf Origin "^(.*\.domain\.com)$" ORIGIN_SUB_DOMAIN=$1 with my domain. But still not working. – fzaman Jan 28 '20 at 09:37