Blocked cross-origin request - PHP

Asked

Viewed 1,200 times

1

Hello!

I know there are several questions on this subject, but even reading the answers I’m still in trouble. I already tried to put Access-Control-Allow-Origin in the file header. php, I’ve tried to put in virtualhost configuration in Apache as well and nothing seems to work. Where am I going wrong?

I’ve seen that if I use an extension or disable browser flags, it works. So maybe it’s something I’m missing on the client side? :/

      Requisição cross-origin bloqueada: A política de mesma origem (Same Origin Policy) impede a leitura do recurso remoto em https://a.4cdn.org/wsg/1.json. (Motivo: o cabeçalho CORS 'Access-Control-Allow-Origin' não está presente).
<html>
    <head>
        <link rel="stylesheet" href="css/trend.css">
        <script src='http://code.jquery.com/jquery-latest.min.js' type='text/javascript'></script>
    </head>
<body>
    <div class="trendings">
        <h3>Trending:</h3>

        <div>           
            <ol id="trending">              
            </ol>
        </div>
    </div>

<script>
    $(document).ready(function(){
        var node,textnode,a,btn;        
        $.getJSON("https://a.4cdn.org/wsg/1.json", function(json) {
            for(var i = 1; i < json.threads.length; i++){
                node = document.createElement("li");
                a = document.createElement('a');
                textnode = document.createTextNode(json.threads[i].posts[0].sub);
                a.appendChild(textnode);
                a.href="http://boards.4chan.org/wsg/thread/"+json.threads[i].posts[0].no;           
                document.getElementById("trending").appendChild(node).appendChild(a);
            }
        });     
    });

</script>
</body>

</html>
  • 2

    Who should "put the header" is the site you are connecting, in case the https://a.4cdn.org/wsg/1.json must have the Access-Control-Allow-Origin: *.

  • there is something I can do to make this request work?

  • 1

    CORS was made to not work. What you can do is a Proxying, your website picks up the content and you take this content from your own website.

  • An example of simple "proxying" with Curl: https://answall.com/a/227766/3635

1 answer

0

Make sure the server accepts external requests, so use tools like Postman. If it works, try adding the following line in your Javascript, before requests:

$.support.Cors = true;

  • This question has been asked at least 25 times, it would be interesting instead of answering something that has already been much debated just use the button to signal and mark questions as well as duplicate: https://answall.com/search?q=qAccess-Control-Allow-Origin+não+está+estápresente

Browser other questions tagged

You are not signed in. Login or sign up in order to post.