0

I am trying to make the following code work on my website (Wordpress - Plugin). But still it is giving error, what can be the reason?

<?php
header("access-control-allow-origin: *");
?>

<div id="latest-trends" style="width: 0px;"></div>

<script type="text/javascript">
$('#latest-trends').load('https://turkcealtyazi.org/index.php #nwrap');
</script>

The error is:

Access to XMLHttpRequest at 'https://turkcealtyazi.org/index.php?_=1612044908947' from origin 'https://yoursite.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

HOY
  • 1,067
  • 10
  • 42
  • 85
  • The header has to be present on the server and not from you. Otherwise it totally defeats the purpose. – Terry Jan 30 '21 at 22:33

1 Answers1

1

You can't send request to another server with Javascript like this. Go with server side solutions.

access-control-allow-origin

header should be provided by server that you try to connect, not you.

tanaydin
  • 5,171
  • 28
  • 45
  • 2
    Haha... I would add that If we could provide the header to the other site just like this, the feature would be useless. To OP: read [the documentation](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin) – Louys Patrice Bessette Jan 30 '21 at 22:25
  • 1
    @tanaydin when using serverside solutions, I face with cloulflare, "checking your browser", and "prove that you are not robot" kind of a message instead of the website that I want to crawl. Looking for an alternate way, let me know if anythings come to your mind. (İstanbuldan selamlar.) – HOY Jan 30 '21 at 23:25
  • maybe that can help. https://stackoverflow.com/questions/38160420/curl-does-not-work-but-same-site-works-with-browser/38160451 (eyvallah) – tanaydin Jan 31 '21 at 17:51