Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

105
Visualizações
Cross-domain POST request with CORS returns access-control-allow-origin is missing

I want to perfrom cross-domain request from localhost to localhost:81, but I constantly get CORS-Header'Access-Control-Allow-Origin' missing. But I have set the header in postHere.php. I also tried header('Access-Control-Allow-Origin: *'); and it is also not working?

postHere.php

<?php
switch ($_SERVER['HTTP_ORIGIN']) {
    case 'http://localhost':
    case 'https://localhost':
        header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
        header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
        header('Access-Control-Max-Age: 1000');
        header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
        break;
}

HTML file in localhost from which I want to send to localhost:81

<html>
    <head>
        <script src="//code.jquery.com/jquery-3.6.0.min.js"></script>
    </head>
    <body>
        <script>
            $.ajax({
            type: 'POST',
            url: 'http://localhost:81/postHere.php',
            crossDomain: true,
            data: '{"some":"json"}',
            dataType: 'json',
            success: function(responseData, textStatus, jqXHR) {
                var value = responseData.someKey;
            },
            error: function (responseData, textStatus, errorThrown) {
                alert('POST failed.');
            }
        });
    </script>
    </body>

</html>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You should enable your server on the second origin (in your case localhost:81, though you should probably use 8081 instead) to allow cross-origin requests. Check mozilla guides on that.

Links: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS https://developer.mozilla.org/en-US/docs/Glossary/CORS

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda